Javatpoint Logo
Javatpoint Logo

Sort Stack using Recursion

A stack is a linear data structure that operates on the Last In First Out (LIFO) principle. This indicates that the last thing added to the stack is deleted first. The alternate word for a stack is LIFO, which refers to the order in which items are removed from it (last in, first out). This form of structure is called a "stack" because it resembles a stack of real goods placed on top of each other. This structure makes it simple to remove one item from the top of the stack, but removing an item from the bottom of the stack may need to remove numerous other things first. A peek action may also provide access to the top of the stack without altering it.

Sorting a stack is useful for various tasks, including memory management, maintaining the context of a process in the event of an interrupt, and other high-priority tasks. Although we shall see the recursive approach here, sorting may also be done iteratively.

Operations in Stack

In a stack, we can perform the following operations, such as:

  • Push(): Pushes an element into the stack
  • Pop(): Pops out the top element of the stack
  • Top(): Returns the top element of the stack
  • Size(): Returns the size of the stack

A stack is a very useful and crucial data structure that is employed in memory management and process flow scheduling. The program counter is one of the most important applications of the stack because it saves the context of a processor code from stacking if it has to transition to a new process so that it may return to the old process and finish it when the new process is completed.

Recursion is one of the most significant algorithms because if we can solve a smaller work, we can almost certainly solve the entire project utilizing the smaller jobs. Recursion is a term that refers to the act of calling oneself. It has a base case, which is the major case, in which it handles the smaller problem scenario before calling itself for the minor sections. It uses the fact that while we are standing in a certain state, we presume that our recursive function has done processing for smaller responses, which we can now combine to solve our present state.

Java Code

Let's write a java code to sort a stack using recursion.

Output

Above java code gives the following output.

Select one of the operations::

1. To insert a new value in the Stack Data Structure.
2. To display the data inside of the Stack Data Structure.
3. To perform sorting (via Recursion) on the Stack Data Structure.
1
Enter integer element to insert
45

Do you want to continue (Type y or n) 

y

Select one of the operations::

1. To insert a new value in the Stack Data Structure.
2. To display the data inside of the Stack Data Structure.
3. To perform sorting (via Recursion) on the Stack Data Structure.
1
Enter integer element to insert
32

Do you want to continue (Type y or n) 

y

Select one of the operations::

1. To insert a new value in the Stack Data Structure.
2. To display the data inside of the Stack Data Structure.
3. To perform sorting (via Recursion) on the Stack Data Structure.
1
Enter integer element to insert
98

Do you want to continue (Type y or n) 

y

Select one of the operations::

1. To insert a new value in the Stack Data Structure.
2. To display the data inside of the Stack Data Structure.
3. To perform sorting (via Recursion) on the Stack Data Structure.
1
Enter integer element to insert
22

Do you want to continue (Type y or n) 

y

Select one of the operations::

1. To insert a new value in the Stack Data Structure.
2. To display the data inside of the Stack Data Structure.
3. To perform sorting (via Recursion) on the Stack Data Structure.
1
Enter integer element to insert
49

Do you want to continue (Type y or n) 

y

Select one of the operations::

1. To insert a new value in the Stack Data Structure.
2. To display the data inside of the Stack Data Structure.
3. To perform sorting (via Recursion) on the Stack Data Structure.
1
Enter integer element to insert
13

Do you want to continue (Type y or n) 

y

Select one of the operations::

1. To insert a new value in the Stack Data Structure.
2. To display the data inside of the Stack Data Structure.
3. To perform sorting (via Recursion) on the Stack Data Structure.
1 
Enter integer element to insert
36

Do you want to continue (Type y or n) 

y

Select one of the operations::

1. To insert a new value in the Stack Data Structure.
2. To display the data inside of the Stack Data Structure.
3. To perform sorting (via Recursion) on the Stack Data Structure.
1
Enter integer element to insert
54

Do you want to continue (Type y or n) 

y

Select one of the operations::

1. To insert a new value in the Stack Data Structure.
2. To display the data inside of the Stack Data Structure.
3. To perform sorting (via Recursion) on the Stack Data Structure.
1
Enter integer element to insert
102

Do you want to continue (Type y or n) 

y

Select one of the operations::

1. To insert a new value in the Stack Data Structure.
2. To display the data inside of the Stack Data Structure.
3. To perform sorting (via Recursion) on the Stack Data Structure.
1
Enter integer element to insert
506

Do you want to continue (Type y or n) 

y

Select one of the operations::

1. To insert a new value in the Stack Data Structure.
2. To display the data inside of the Stack Data Structure.
3. To perform sorting (via Recursion) on the Stack Data Structure.
2
Stack:: [45, 32, 98, 22, 49, 13, 36, 54, 102, 506]

Do you want to continue (Type y or n) 

y

Select one of the operations::

1. To insert a new value in the Stack Data Structure.
2. To display the data inside of the Stack Data Structure.
3. To perform sorting (via Recursion) on the Stack Data Structure.
3
Sort done.

Do you want to continue (Type y or n) 

y

Select one of the operations::
1. To insert a new value in the Stack Data Structure.
2. To display the data inside of the Stack Data Structure.
3. To perform sorting (via Recursion) on the Stack Data Structure.
2
Stack:: [13, 22, 32, 36, 45, 49, 54, 98, 102, 506]

Do you want to continue (Type y or n) 

n

In this way, we have written Java code to perform the sort operation on the stack data structure. There are three functions written for the stack data structure one is to add a new node in the stack data structure, the second one is to display all the content present in the stack data structure, and third and the last function is to perform the sort operation on the stack data structure.

The user first adds sufficient data in the stack data structure. Once the nodes are added successfully, the sort operation is performed on the stack data structure by selecting the third option from the menu displayed after each operation which will call the sort() function written in the code to which pass the object of the stack data structure as a parameter.

After the successful completion of the sort operation, the user can confirm the result of the sort operation by displaying the values of the nodes in the stack data structure by selecting the second option from the user. Once all the operations are done, the user can exit the code by entering the 'n' or 'N' character.

C++ Code

Now let's write a C++ code to sort a stack using recursion.

Output

The above C++ code gives the following output.

Select one of the operations::
1. To insert a new node in the Stack Data Structure.
2. To display the nodes of the Stack Data Structure.
3. To perform sorting on the Stack Data Structure.
1
Enter the value to be inserted
45

Do you want to continue (Type y or n) 
y

Select one of the operations::
1. To insert a new node in the Stack Data Structure.
2. To display the nodes of the Stack Data Structure.
3. To perform sorting on the Stack Data Structure.
1
Enter the value to be inserted
32

Do you want to continue (Type y or n) 
y

Select one of the operations::
1. To insert a new node in the Stack Data Structure.
2. To display the nodes of the Stack Data Structure.
3. To perform sorting on the Stack Data Structure.
1
Enter the value to be inserted
97 

Do you want to continue (Type y or n) 
y

Select one of the operations::
1. To insert a new node in the Stack Data Structure.
2. To display the nodes of the Stack Data Structure.
3. To perform sorting on the Stack Data Structure.
1
Enter the value to be inserted
40

Do you want to continue (Type y or n) 
y

Select one of the operations::
1. To insert a new node in the Stack Data Structure.
2. To display the nodes of the Stack Data Structure.
3. To perform sorting on the Stack Data Structure.
1
Enter the value to be inserted
21

Do you want to continue (Type y or n) 
y

Select one of the operations::
1. To insert a new node in the Stack Data Structure.
2. To display the nodes of the Stack Data Structure.
3. To perform sorting on the Stack Data Structure.
1
Enter the value to be inserted
67

Do you want to continue (Type y or n) 
y

Select one of the operations::
1. To insert a new node in the Stack Data Structure.
2. To display the nodes of the Stack Data Structure.
3. To perform sorting on the Stack Data Structure.
1
Enter the value to be inserted
30

Do you want to continue (Type y or n) 
y
Select one of the operations::
1. To insert a new node in the Stack Data Structure.
2. To display the nodes of the Stack Data Structure.
3. To perform sorting on the Stack Data Structure.
1
Enter the value to be inserted
505

Do you want to continue (Type y or n) 
y

Select one of the operations::
1. To insert a new node in the Stack Data Structure.
2. To display the nodes of the Stack Data Structure.
3. To perform sorting on the Stack Data Structure.
1
Enter the value to be inserted
672

Do you want to continue (Type y or n) 
y

Select one of the operations::
1. To insert a new node in the Stack Data Structure.
2. To display the nodes of the Stack Data Structure.
3. To perform sorting on the Stack Data Structure.
1
Enter the value to be inserted
341

Do you want to continue (Type y or n) 
y

Select one of the operations::
1. To insert a new node in the Stack Data Structure.
2. To display the nodes of the Stack Data Structure.
3. To perform sorting on the Stack Data Structure.
1
Enter the value to be inserted
231

Do you want to continue (Type y or n) 
y

Select one of the operations::
1. To insert a new node in the Stack Data Structure.
2. To display the nodes of the Stack Data Structure.
3. To perform sorting on the Stack Data Structure.
2
Contents of the stack are::
231 341 672 505 30 67 21 40 97 32 45 

Do you want to continue (Type y or n) 
y

Select one of the operations::
1. To insert a new node in the Stack Data Structure.
2. To display the nodes of the Stack Data Structure.
3. To perform sorting on the Stack Data Structure.
3
Sort did successfully.

Do you want to continue (Type y or n) 
y

Select one of the operations::
1. To insert a new node in the Stack Data Structure.
2. To display the nodes of the Stack Data Structure.
3. To perform sorting on the Stack Data Structure.
2
Contents of the stack are::
672 505 341 231 97 67 45 40 32 30 21 

Do you want to continue (Type y or n) 
n

In this way, we have written C++ code to perform the sort operation on the stack data structure. There are three functions written for the stack data structure one is to add a new node in the stack data structure, the second one is to display all the content present in the stack data structure, and third and the last function is to perform the sort operation on the stack data structure.

The user first adds sufficient data in the stack data structure. Once the nodes are added successfully, the sort operation is performed on the stack data structure by selecting the third option from the menu displayed after each operation which will call the sort() function written in the code to which pass the object of the stack data structure as a parameter.

After the successful completion of the sort operation, the user can confirm the result of the sort operation by displaying the values of the nodes in the stack data structure by selecting the second option from the user. Once all the operations are done, the user can exit the code by entering the 'n' or 'N' character.

C Code

Now let's write a simple C code to Sort a stack using recursion.

Output

The above C code gives this output.

Select one of the operations::
1. To insert a new node in the Stack Data Structure.
2. To display the nodes of the Stack Data Structure.
3. To perform a sort on the Stack Data Structure.
1

Enter the value to be inserted
12

Do you want to continue (Type y or n)
y

Select one of the operations::
1. To insert a new node in the Stack Data Structure.
2. To display the nodes of the Stack Data Structure.
3. To perform a sort on the Stack Data Structure.
1

Enter the value to be inserted
45

Do you want to continue (Type y or n)
y

Select one of the operations::
1. To insert a new node in the Stack Data Structure.
2. To display the nodes of the Stack Data Structure.
3. To perform a sort on the Stack Data Structure.
1

Enter the value to be inserted
9

Do you want to continue (Type y or n)
y

Select one of the operations::
1. To insert a new node in the Stack Data Structure.
2. To display the nodes of the Stack Data Structure.
3. To perform a sort on the Stack Data Structure.
1

Enter the value to be inserted
32

Do you want to continue (Type y or n)
y

Select one of the operations::
1. To insert a new node in the Stack Data Structure.
2. To display the nodes of the Stack Data Structure.
3. To perform a sort on the Stack Data Structure.
1

Enter the value to be inserted
87

Do you want to continue (Type y or n)
y

Select one of the operations::
1. To insert a new node in the Stack Data Structure.
2. To display the nodes of the Stack Data Structure.
3. To perform a sort on the Stack Data Structure.
1

Enter the value to be inserted
20

Do you want to continue (Type y or n)
y

Select one of the operations::
1. To insert a new node in the Stack Data Structure.
2. To display the nodes of the Stack Data Structure.
3. To perform a sort on the Stack Data Structure.
1

Enter the value to be inserted
18

Do you want to continue (Type y or n)
y

Select one of the operations::
1. To insert a new node in the Stack Data Structure.
2. To display the nodes of the Stack Data Structure.
3. To perform a sort on the Stack Data Structure.
1

Enter the value to be inserted
56

Do you want to continue (Type y or n)
y

Select one of the operations::
1. To insert a new node in the Stack Data Structure.
2. To display the nodes of the Stack Data Structure.
3. To perform a sort on the Stack Data Structure.
2
Stack elements:
56 18 20 87 32 9 45 12 

Do you want to continue (Type y or n)
y

Select one of the operations::
1. To insert a new node in the Stack Data Structure.
2. To display the nodes of the Stack Data Structure.
3. To perform a sort on the Stack Data Structure.
3

Sort applied successfully.

Do you want to continue (Type y or n)
y

Select one of the operations::
1. To insert a new node in the Stack Data Structure.
2. To display the nodes of the Stack Data Structure.
3. To perform a sort on the Stack Data Structure.
2
Stack elements:
87 56 45 32 20 18 12 9 

Do you want to continue (Type y or n)
N

In this way, we have written C code to perform the sort operation on the stack data structure. There are three functions written for the stack data structure one is to add a new node in the stack data structure, the second one is to display all the content present in the stack data structure, and third and the last function is to perform the sort operation on the stack data structure.

The user first adds sufficient data in the stack data structure. Once the nodes are added successfully, the sort operation is performed on the stack data structure by selecting the third option from the menu displayed after each operation which will call the sort() function written in the code to which pass the object of the stack data structure as a parameter.

After the successful completion of the sort operation, the user can confirm the result of the sort operation by displaying the values of the nodes in the stack data structure by selecting the second option from the user. Once all the operations are done, the user can exit the code by entering the 'n' or 'N' character.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA