Given a stack, sort it using recursion. Use of any loop constructs like while, for..etc is not allowed. We can only use the following ADT functions on Stack S:

is_empty(S)  : Tests whether stack is empty or not.
push(S)	     : Adds new element to the stack.
pop(S)	     : Removes top element from the stack.
top(S)	     : Returns value of the top element. Note that this
               function does not remove element from the stack.

Example:

Input:  -3  <--- Top
        14 
        18 
        -5 
        30 

Output: 30  <--- Top
        18 
        14 
        -3 
        -5

We strongly recommend you to minimize your browser and try this yourself first.
This problem is mainly a variant of Reverse stack using recursion.

The idea of the solution is to hold all values in Function Call Stack until the stack becomes empty. When the stack becomes empty, insert all held items one by one in sorted order. Here sorted order is important.

[ad type=”banner”]
Algorithm

We can use below algorithm to sort stack elements:

sortStack(stack S)
	if stack is not empty:
		temp = pop(S);  
		sortStack(S); 
		sortedInsert(S, temp);

Below algorithm is to insert element is sorted order:

sortedInsert(Stack S, element)
	if stack is empty OR element > top element
		push(S, elem)
	else
		temp = pop(S)
		sortedInsert(S, element)
		push(S, temp)
[ad type=”banner”]
Illustration:

Let given stack be
-3	<-- top of the stack
14
18
-5
30

Let us illustrate sorting of stack using above example:

First pop all the elements from the stack and store poped element in variable ‘temp’. After poping all the elements function’s stack frame will look like:

temp = -3	--> stack frame #1
temp = 14	--> stack frame #2
temp = 18	--> stack frame #3
temp = -5	--> stack frame #4
temp = 30       --> stack frame #5

Now stack is empty and ‘insert_in_sorted_order()’ function is called and it inserts 30 (from stack frame #5) at the bottom of the stack. Now stack looks like below:

30	<-- top of the stack

Now next element i.e. -5 (from stack frame #4) is picked. Since -5 < 30, -5 is inserted at the bottom of stack. Now stack becomes:

30	<-- top of the stack
-5

Next 18 (from stack frame #3) is picked. Since 18 < 30, 18 is inserted below 30. Now stack becomes:

30	<-- top of the stack
18	
-5
[ad type=”banner”]

Next 14 (from stack frame #2) is picked. Since 14 < 30 and 14 < 18, it is inserted below 18. Now stack becomes:

30	<-- top of the stack
18
14	
-5

Now -3 (from stack frame #1) is picked, as -3 < 30 and -3 < 18 and -3 < 14, it is inserted below 14. Now stack becomes:

30	<-- top of the stack
18
14
-3	
-5


Implementation:

Below is C and Java implementation of above algorithm.

[ad type=”banner”]

C Programming:

[pastacode lang=”c” manual=”%2F%2F%20C%20program%20to%20sort%20a%20stack%20using%20recursion%0A%23include%20%3Cstdio.h%3E%0A%23include%20%3Cstdlib.h%3E%0A%20%0A%2F%2F%20Stack%20is%20represented%20using%20linked%20list%0Astruct%20stack%0A%7B%0A%20%20%20%20int%20data%3B%0A%20%20%20%20struct%20stack%20*next%3B%0A%7D%3B%0A%20%0A%2F%2F%20Utility%20function%20to%20initialize%20stack%0Avoid%20initStack(struct%20stack%20**s)%0A%7B%0A%20%20%20%20*s%20%3D%20NULL%3B%0A%7D%0A%20%0A%2F%2F%20Utility%20function%20to%20chcek%20if%20stack%20is%20empty%0Aint%20isEmpty(struct%20stack%20*s)%0A%7B%0A%20%20%20%20if%20(s%20%3D%3D%20NULL)%0A%20%20%20%20%20%20%20%20return%201%3B%0A%20%20%20%20return%200%3B%0A%7D%0A%20%0A%2F%2F%20Utility%20function%20to%20push%20an%20item%20to%20stack%0Avoid%20push(struct%20stack%20**s%2C%20int%20x)%0A%7B%0A%20%20%20%20struct%20stack%20*p%20%3D%20(struct%20stack%20*)malloc(sizeof(*p))%3B%0A%20%0A%20%20%20%20if%20(p%20%3D%3D%20NULL)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20fprintf(stderr%2C%20%22Memory%20allocation%20failed.%5Cn%22)%3B%0A%20%20%20%20%20%20%20%20return%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20p-%3Edata%20%3D%20x%3B%0A%20%20%20%20p-%3Enext%20%3D%20*s%3B%0A%20%20%20%20*s%20%3D%20p%3B%0A%7D%0A%20%0A%2F%2F%20Utility%20function%20to%20remove%20an%20item%20from%20stack%0Aint%20pop(struct%20stack%20**s)%0A%7B%0A%20%20%20%20int%20x%3B%0A%20%20%20%20struct%20stack%20*temp%3B%0A%20%0A%20%20%20%20x%20%3D%20(*s)-%3Edata%3B%0A%20%20%20%20temp%20%3D%20*s%3B%0A%20%20%20%20(*s)%20%3D%20(*s)-%3Enext%3B%0A%20%20%20%20free(temp)%3B%0A%20%0A%20%20%20%20return%20x%3B%0A%7D%0A%20%0A%2F%2F%20Function%20to%20find%20top%20item%0Aint%20top(struct%20stack%20*s)%0A%7B%0A%20%20%20%20return%20(s-%3Edata)%3B%0A%7D%0A%20%0A%2F%2F%20Recursive%20function%20to%20insert%20an%20item%20x%20in%20sorted%20way%0Avoid%20sortedInsert(struct%20stack%20**s%2C%20int%20x)%0A%7B%0A%20%20%20%20%2F%2F%20Base%20case%3A%20Either%20stack%20is%20empty%20or%20newly%20inserted%0A%20%20%20%20%2F%2F%20item%20is%20greater%20than%20top%20(more%20than%20all%20existing)%0A%20%20%20%20if%20(isEmpty(*s)%20%7C%7C%20x%20%3E%20top(*s))%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20push(s%2C%20x)%3B%0A%20%20%20%20%20%20%20%20return%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20If%20top%20is%20greater%2C%20remove%20the%20top%20item%20and%20recur%0A%20%20%20%20int%20temp%20%3D%20pop(s)%3B%0A%20%20%20%20sortedInsert(s%2C%20x)%3B%0A%20%0A%20%20%20%20%2F%2F%20Put%20back%20the%20top%20item%20removed%20earlier%0A%20%20%20%20push(s%2C%20temp)%3B%0A%7D%0A%20%0A%2F%2F%20Function%20to%20sort%20stack%0Avoid%20sortStack(struct%20stack%20**s)%0A%7B%0A%20%20%20%20%2F%2F%20If%20stack%20is%20not%20empty%0A%20%20%20%20if%20(!isEmpty(*s))%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Remove%20the%20top%20item%0A%20%20%20%20%20%20%20%20int%20x%20%3D%20pop(s)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Sort%20remaining%20stack%0A%20%20%20%20%20%20%20%20sortStack(s)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Push%20the%20top%20item%20back%20in%20sorted%20stack%0A%20%20%20%20%20%20%20%20sortedInsert(s%2C%20x)%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F%2F%20Utility%20function%20to%20print%20contents%20of%20stack%0Avoid%20printStack(struct%20stack%20*s)%0A%7B%0A%20%20%20%20while%20(s)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20printf(%22%25d%20%22%2C%20s-%3Edata)%3B%0A%20%20%20%20%20%20%20%20s%20%3D%20s-%3Enext%3B%0A%20%20%20%20%7D%0A%20%20%20%20printf(%22%5Cn%22)%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20Program%0Aint%20main(void)%0A%7B%0A%20%20%20%20struct%20stack%20*top%3B%0A%20%0A%20%20%20%20initStack(%26top)%3B%0A%20%20%20%20push(%26top%2C%2030)%3B%0A%20%20%20%20push(%26top%2C%20-5)%3B%0A%20%20%20%20push(%26top%2C%2018)%3B%0A%20%20%20%20push(%26top%2C%2014)%3B%0A%20%20%20%20push(%26top%2C%20-3)%3B%0A%20%0A%20%20%20%20printf(%22Stack%20elements%20before%20sorting%3A%5Cn%22)%3B%0A%20%20%20%20printStack(top)%3B%0A%20%0A%20%20%20%20sortStack(%26top)%3B%0A%20%20%20%20printf(%22%5Cn%5Cn%22)%3B%0A%20%0A%20%20%20%20printf(%22Stack%20elements%20after%20sorting%3A%5Cn%22)%3B%0A%20%20%20%20printStack(top)%3B%0A%20%0A%20%20%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

Output:

Stack elements before sorting:
-3 14 18 -5 30 

Stack elements after sorting:
30 18 14 -3 -5

Exercise: Modify above code to reverse stack in descending order.

[ad type=”banner”]