Merging and Sorting Two Unsorted Stacks | Java program to merge to unsorted stacks into a third stack in sorted way . ; This is the temporary stack ; Sorts input stack and returns sorted stack . ; pop out the first element ; while temporary stack is not empty and top of stack is greater than temp ; pop from temporary stack and push it to the input stack ; push temp in temporary of stack ; Push contents of both stacks in result ; Sort the result stack . ; main function
import java . io . * ; import java . util . * ; public class GFG { static Stack < Integer > res = new Stack < Integer > ( ) ; static Stack < Integer > tmpStack = new Stack < Integer > ( ) ; static void sortStack ( Stack < Integer > input ) { while ( input . size ( ) != 0 ) { int tmp = input . peek ( ) ; input . pop ( ) ; while ( tmpStack . size ( ) != 0 && tmpStack . peek ( ) > tmp ) { input . push ( tmpStack . peek ( ) ) ; tmpStack . pop ( ) ; } tmpStack . push ( tmp ) ; } } static void sortedMerge ( Stack < Integer > s1 , Stack < Integer > s2 ) { while ( s1 . size ( ) != 0 ) { res . push ( s1 . peek ( ) ) ; s1 . pop ( ) ; } while ( s2 . size ( ) != 0 ) { res . push ( s2 . peek ( ) ) ; s2 . pop ( ) ; } sortStack ( res ) ; } public static void main ( String args [ ] ) { Stack < Integer > s1 = new Stack < Integer > ( ) ; Stack < Integer > s2 = new Stack < Integer > ( ) ; s1 . push ( 34 ) ; s1 . push ( 3 ) ; s1 . push ( 31 ) ; s2 . push ( 1 ) ; s2 . push ( 12 ) ; s2 . push ( 23 ) ; sortedMerge ( s1 , s2 ) ; System . out . println ( " Sorted ▁ and ▁ merged ▁ stack ▁ : " ) ; while ( tmpStack . size ( ) != 0 ) { System . out . print ( tmpStack . peek ( ) + " ▁ " ) ; tmpStack . pop ( ) ; } } }