Given an array of elements, task is to sort these elements using stack.
Prerequisites : Stacks
Input : 8 5 7 1 9 12 10 Output : 1 5 7 8 9 10 12 Explanation : Output is sorted element set Input : 7 4 10 20 2 5 9 1 Output : 1 2 4 5 7 9 10 20
We basically use Sort a stack using a temporary stack. Then we put sorted stack elements back to array.
5 10 15 45
Time Complexity : O(n*n)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Merging and Sorting Two Unsorted Stacks
- Sorting objects using In-Place sorting algorithm
- Know Your Sorting Algorithm | Set 1 (Sorting Weapons used by Programming Languages)
- Know Your Sorting Algorithm | Set 2 (Introsort- C++’s Sorting Weapon)
- Queue using Stacks
- Iterative Postorder Traversal | Set 1 (Using Two Stacks)
- Bubble sort using two Stacks
- Infix to Prefix conversion using two stacks
- Implement two stacks in an array
- How to efficiently implement k stacks in a single array?
- Sorting an array according to another array using pair in STL
- Find maximum sum possible equal sum of three stacks
- Sudo Placement[1.3] | Playing with Stacks
- Add two numbers represented by Stacks
- Check if the two given stacks are same
- Maximize sum of topmost elements of S stacks by popping at most N elements
- Sorting Array Elements By Frequency | Set 3 (Using STL)
- Sorting array of strings (or words) using Trie
- Sorting array of strings (or words) using Trie | Set-2 (Handling Duplicates)
- Sorting Strings using Bubble Sort
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.