# How to Sort a Stack using Recursion

• Difficulty Level : Medium
• Last Updated : 17 Aug, 2022

Given a stack, the task is to sort it using recursion.

Example:

Input: elements present in stack from top to bottom -3 14 18 -5 30
Output: 30 18 14 -3 -5
Explanation: The given stack is sorted know 30 > 18 > 14 > -3 > -5

Input: elements present in stack from top to bottom 1 2 3
Output: 3 2 1
Explanation: The given stack is sorted know 3 > 2 > 1

## How to Sort a Stack Using Stack

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. and then print the stack

Illustration:

Below is the illustration of above approach

• Let given stack be
• Let us illustrate sorting of stack using the above example:
First pop all the elements from the stack and store popped element in the variable ‘temp’. After popping all the elements function’s stack frame will look like this:

• Now stack is empty so function insert in sorted order is called and it inserts 30 (from stack frame 5) at the bottom of the stack. Now stack looks like the below:
• Now next element  -5 (from stack frame 4) is picked. Since -5 < 30, -5 is inserted at the bottom of the stack. Now stack becomes:
• Next 18 (from stack frame 3) is picked. Since 18 < 30, 18 is inserted below 30. Now stack becomes:
• Next 14 (from stack frame 2) is picked. Since 14 < 30 and 14 < 18, it is inserted below 18. Now stack becomes:
• 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:

Follow the steps mentioned below to implement the idea:

• Create a stack and push all the elements in it.
• Call sortStack(), which will pop an element from the stack and pass the popped element to function sortInserted(), then it will keep calling itself until the stack is empty.
• Whenever sortInserted() is called it will insert the passed element in stack in sorted order.
• Print the stack

Below is the implementation of the above approach:

## C++

 `// C++ program to sort a stack using recursion` `#include ` `using` `namespace` `std;`   `// Stack is represented using linked list` `struct` `stack {` `    ``int` `data;` `    ``struct` `stack* next;` `};`   `// Utility function to initialize stack` `void` `initStack(``struct` `stack** s) { *s = NULL; }`   `// Utility function to check if stack is empty` `int` `isEmpty(``struct` `stack* s)` `{` `    ``if` `(s == NULL)` `        ``return` `1;` `    ``return` `0;` `}`   `// Utility function to push an item to stack` `void` `push(``struct` `stack** s, ``int` `x)` `{` `    ``struct` `stack* p = (``struct` `stack*)``malloc``(``sizeof``(*p));`   `    ``if` `(p == NULL) {` `        ``fprintf``(stderr, ``"Memory allocation failed.\n"``);` `        ``return``;` `    ``}`   `    ``p->data = x;` `    ``p->next = *s;` `    ``*s = p;` `}`   `// Utility function to remove an item from stack` `int` `pop(``struct` `stack** s)` `{` `    ``int` `x;` `    ``struct` `stack* temp;`   `    ``x = (*s)->data;` `    ``temp = *s;` `    ``(*s) = (*s)->next;` `    ``free``(temp);`   `    ``return` `x;` `}`   `// Function to find top item` `int` `top(``struct` `stack* s) { ``return` `(s->data); }`   `// Recursive function to insert an item x in sorted way` `void` `sortedInsert(``struct` `stack** s, ``int` `x)` `{` `    ``// Base case: Either stack is empty or newly inserted` `    ``// item is greater than top (more than all existing)` `    ``if` `(isEmpty(*s) or x > top(*s)) {` `        ``push(s, x);` `        ``return``;` `    ``}`   `    ``// If top is greater, remove the top item and recur` `    ``int` `temp = pop(s);` `    ``sortedInsert(s, x);`   `    ``// Put back the top item removed earlier` `    ``push(s, temp);` `}`   `// Function to sort stack` `void` `sortStack(``struct` `stack** s)` `{` `    ``// If stack is not empty` `    ``if` `(!isEmpty(*s)) {` `        ``// Remove the top item` `        ``int` `x = pop(s);`   `        ``// Sort remaining stack` `        ``sortStack(s);`   `        ``// Push the top item back in sorted stack` `        ``sortedInsert(s, x);` `    ``}` `}`   `// Utility function to print contents of stack` `void` `printStack(``struct` `stack* s)` `{` `    ``while` `(s) {` `        ``cout << s->data << ``" "``;` `        ``s = s->next;` `    ``}` `    ``cout << ``"\n"``;` `}`   `// Driver code` `int` `main(``void``)` `{` `    ``struct` `stack* top;`   `    ``initStack(&top);` `    ``push(&top, 30);` `    ``push(&top, -5);` `    ``push(&top, 18);` `    ``push(&top, 14);` `    ``push(&top, -3);`   `    ``cout << ``"Stack elements before sorting:\n"``;` `    ``printStack(top);`   `    ``sortStack(&top);` `    ``cout << ``"\n"``;`   `    ``cout << ``"Stack elements after sorting:\n"``;` `    ``printStack(top);`   `    ``return` `0;` `}`   `// This code is contributed by SHUBHAMSINGH10`

## C

 `// C program to sort a stack using recursion` `#include ` `#include `   `// Stack is represented using linked list` `struct` `stack {` `    ``int` `data;` `    ``struct` `stack* next;` `};`   `// Utility function to initialize stack` `void` `initStack(``struct` `stack** s) { *s = NULL; }`   `// Utility function to check if stack is empty` `int` `isEmpty(``struct` `stack* s)` `{` `    ``if` `(s == NULL)` `        ``return` `1;` `    ``return` `0;` `}`   `// Utility function to push an item to stack` `void` `push(``struct` `stack** s, ``int` `x)` `{` `    ``struct` `stack* p = (``struct` `stack*)``malloc``(``sizeof``(*p));`   `    ``if` `(p == NULL) {` `        ``fprintf``(stderr, ``"Memory allocation failed.\n"``);` `        ``return``;` `    ``}`   `    ``p->data = x;` `    ``p->next = *s;` `    ``*s = p;` `}`   `// Utility function to remove an item from stack` `int` `pop(``struct` `stack** s)` `{` `    ``int` `x;` `    ``struct` `stack* temp;`   `    ``x = (*s)->data;` `    ``temp = *s;` `    ``(*s) = (*s)->next;` `    ``free``(temp);`   `    ``return` `x;` `}`   `// Function to find top item` `int` `top(``struct` `stack* s) { ``return` `(s->data); }`   `// Recursive function to insert an item x in sorted way` `void` `sortedInsert(``struct` `stack** s, ``int` `x)` `{` `    ``// Base case: Either stack is empty or newly inserted` `    ``// item is greater than top (more than all existing)` `    ``if` `(isEmpty(*s) || x > top(*s)) {` `        ``push(s, x);` `        ``return``;` `    ``}`   `    ``// If top is greater, remove the top item and recur` `    ``int` `temp = pop(s);` `    ``sortedInsert(s, x);`   `    ``// Put back the top item removed earlier` `    ``push(s, temp);` `}`   `// Function to sort stack` `void` `sortStack(``struct` `stack** s)` `{` `    ``// If stack is not empty` `    ``if` `(!isEmpty(*s)) {` `        ``// Remove the top item` `        ``int` `x = pop(s);`   `        ``// Sort remaining stack` `        ``sortStack(s);`   `        ``// Push the top item back in sorted stack` `        ``sortedInsert(s, x);` `    ``}` `}`   `// Utility function to print contents of stack` `void` `printStack(``struct` `stack* s)` `{` `    ``while` `(s) {` `        ``printf``(``"%d "``, s->data);` `        ``s = s->next;` `    ``}` `    ``printf``(``"\n"``);` `}`   `// Driver code` `int` `main(``void``)` `{` `    ``struct` `stack* top;`   `    ``initStack(&top);` `    ``push(&top, 30);` `    ``push(&top, -5);` `    ``push(&top, 18);` `    ``push(&top, 14);` `    ``push(&top, -3);`   `    ``printf``(``"Stack elements before sorting:\n"``);` `    ``printStack(top);`   `    ``sortStack(&top);` `    ``printf``(``"\n\n"``);`   `    ``printf``(``"Stack elements after sorting:\n"``);` `    ``printStack(top);`   `    ``return` `0;` `}`

## Java

 `// Java program to sort a Stack using recursion` `// Note that here predefined Stack class is used` `// for stack operation`   `import` `java.util.ListIterator;` `import` `java.util.Stack;`   `class` `Test {` `    ``// Recursive Method to insert an item x in sorted way` `    ``static` `void` `sortedInsert(Stack s, ``int` `x)` `    ``{` `        ``// Base case: Either stack is empty or newly` `        ``// inserted item is greater than top (more than all` `        ``// existing)` `        ``if` `(s.isEmpty() || x > s.peek()) {` `            ``s.push(x);` `            ``return``;` `        ``}`   `        ``// If top is greater, remove the top item and recur` `        ``int` `temp = s.pop();` `        ``sortedInsert(s, x);`   `        ``// Put back the top item removed earlier` `        ``s.push(temp);` `    ``}`   `    ``// Method to sort stack` `    ``static` `void` `sortStack(Stack s)` `    ``{` `        ``// If stack is not empty` `        ``if` `(!s.isEmpty()) {` `            ``// Remove the top item` `            ``int` `x = s.pop();`   `            ``// Sort remaining stack` `            ``sortStack(s);`   `            ``// Push the top item back in sorted stack` `            ``sortedInsert(s, x);` `        ``}` `    ``}`   `    ``// Utility Method to print contents of stack` `    ``static` `void` `printStack(Stack s)` `    ``{` `        ``ListIterator lt = s.listIterator();`   `        ``// forwarding` `        ``while` `(lt.hasNext())` `            ``lt.next();`   `        ``// printing from top to bottom` `        ``while` `(lt.hasPrevious())` `            ``System.out.print(lt.previous() + ``" "``);` `    ``}`   `    ``// Driver code` `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``Stack s = ``new` `Stack<>();` `        ``s.push(``30``);` `        ``s.push(-``5``);` `        ``s.push(``18``);` `        ``s.push(``14``);` `        ``s.push(-``3``);`   `        ``System.out.println(` `            ``"Stack elements before sorting: "``);` `        ``printStack(s);`   `        ``sortStack(s);`   `        ``System.out.println(` `            ``" \n\nStack elements after sorting:"``);` `        ``printStack(s);` `    ``}` `}`

## Python3

 `# Python program to sort a stack using recursion`   `# Recursive method to insert element in sorted way`     `def` `sortedInsert(s, element):`   `    ``# Base case: Either stack is empty or newly inserted` `    ``# item is greater than top (more than all existing)` `    ``if` `len``(s) ``=``=` `0` `or` `element > s[``-``1``]:` `        ``s.append(element)` `        ``return` `    ``else``:`   `        ``# Remove the top item and recur` `        ``temp ``=` `s.pop()` `        ``sortedInsert(s, element)`   `        ``# Put back the top item removed earlier` `        ``s.append(temp)`   `# Method to sort stack`     `def` `sortStack(s):`   `    ``# If stack is not empty` `    ``if` `len``(s) !``=` `0``:`   `        ``# Remove the top item` `        ``temp ``=` `s.pop()`   `        ``# Sort remaining stack` `        ``sortStack(s)`   `        ``# Push the top item back in sorted stack` `        ``sortedInsert(s, temp)`   `# Printing contents of stack`     `def` `printStack(s):` `    ``for` `i ``in` `s[::``-``1``]:` `        ``print``(i, end``=``" "``)` `    ``print``()`     `# Driver Code` `if` `__name__ ``=``=` `'__main__'``:` `    ``s ``=` `[]` `    ``s.append(``30``)` `    ``s.append(``-``5``)` `    ``s.append(``18``)` `    ``s.append(``14``)` `    ``s.append(``-``3``)`   `    ``print``(``"Stack elements before sorting: "``)` `    ``printStack(s)`   `    ``sortStack(s)`   `    ``print``(``"\nStack elements after sorting: "``)` `    ``printStack(s)`   `# This code is contributed by Muskan Kalra.`

## C#

 `// C# program to sort a Stack using recursion` `// Note that here predefined Stack class is used` `// for stack operation` `using` `System;` `using` `System.Collections;`   `public` `class` `GFG {` `    ``// Recursive Method to insert an item x in sorted way` `    ``static` `void` `sortedInsert(Stack s, ``int` `x)` `    ``{` `        ``// Base case: Either stack is empty or` `        ``// newly inserted item is greater than top` `        ``// (more than all existing)` `        ``if` `(s.Count == 0 || x > (``int``)s.Peek()) {` `            ``s.Push(x);` `            ``return``;` `        ``}`   `        ``// If top is greater, remove` `        ``// the top item and recur` `        ``int` `temp = (``int``)s.Peek();` `        ``s.Pop();` `        ``sortedInsert(s, x);`   `        ``// Put back the top item removed earlier` `        ``s.Push(temp);` `    ``}`   `    ``// Method to sort stack` `    ``static` `void` `sortStack(Stack s)` `    ``{` `        ``// If stack is not empty` `        ``if` `(s.Count > 0) {` `            ``// Remove the top item` `            ``int` `x = (``int``)s.Peek();` `            ``s.Pop();`   `            ``// Sort remaining stack` `            ``sortStack(s);`   `            ``// Push the top item back in sorted stack` `            ``sortedInsert(s, x);` `        ``}` `    ``}`   `    ``// Utility Method to print contents of stack` `    ``static` `void` `printStack(Stack s)` `    ``{` `        ``foreach``(``int` `c ``in` `s) { Console.Write(c + ``" "``); }` `    ``}`   `    ``// Driver code` `    ``public` `static` `void` `Main(String[] args)` `    ``{` `        ``Stack s = ``new` `Stack();` `        ``s.Push(30);` `        ``s.Push(-5);` `        ``s.Push(18);` `        ``s.Push(14);` `        ``s.Push(-3);`   `        ``Console.WriteLine(` `            ``"Stack elements before sorting: "``);` `        ``printStack(s);`   `        ``sortStack(s);`   `        ``Console.WriteLine(` `            ``" \n\nStack elements after sorting:"``);` `        ``printStack(s);` `    ``}` `}`   `// This code is Contributed by Arnab Kundu`

## Javascript

 ``

Output

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

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

Time Complexity: O(N2).
Auxiliary Space: O(N) use of Stack

My Personal Notes arrow_drop_up
Recommended Articles
Page :