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
- 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
- Know Your Sorting Algorithm | Set 1 (Sorting Weapons used by Programming Languages)
- 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)
- 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 objects using In-Place sorting algorithm
- Sorting element of an array by frequency in decreasing order
- 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
- Check if the two given stacks are same
- Queue using Stacks
- Bubble sort using two Stacks
- Sudo Placement[1.3] | Playing with 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 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.