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)
- Merging and Sorting Two Unsorted Stacks
- Implement two stacks in an array
- How to efficiently implement k stacks in a single array?
- Sorting all array elements except one
- Row wise sorting in 2D array
- Sorting an array according to another array using pair in STL
- Sorting array except elements in a subarray
- Sorting Array Elements By Frequency | Set 3 (Using STL)
- Know Your Sorting Algorithm | Set 1 (Sorting Weapons used by Programming Languages)
- Sorting array elements with set bits equal to K
- Sorting array of strings (or words) using Trie
- Know Your Sorting Algorithm | Set 2 (Introsort- C++’s Sorting Weapon)
- Sorting array of strings (or words) using Trie | Set-2 (Handling Duplicates)
- Find the Minimum length Unsorted Subarray, sorting which makes the complete array sorted
- Queue using Stacks
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.
Improved By : manishshaw1