Prerequisite : Bubble Sort
Write a function that sort an array of integers using stacks and also uses bubble sort paradigm.
1. Push all elements of array in 1st stack 2. Run a loop for 'n' times(n is size of array) having the following : 2.a. Keep on pushing elements in the 2nd stack till the top of second stack is smaller than element being pushed from 1st stack. 2.b. If the element being pushed is smaller than top of 2nd stack then swap them (as in bubble sort) *Do above steps alternatively TRICKY STEP: Once a stack is empty, then the top of the next stack will be the largest number so keep it at its position in array i.e arr[len-1-i] and then pop it from that stack.
[2, 5, 10, 12, 15, 44]
This article is contributed by Gaurav Miglani and Abhishek Somani . 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Comparison among Bubble Sort, Selection Sort and Insertion Sort
- Bubble Sort
- C Program for Bubble Sort
- C++ Program for Bubble Sort
- Recursive Bubble Sort
- Java Program for Bubble Sort
- Python Program for Bubble Sort
- Sorting Strings using Bubble Sort
- C++ Program for Recursive Bubble Sort
- Sorting Algorithms Visualization : Bubble Sort
- C Program for Bubble Sort on Linked List
- Bubble Sort On Doubly Linked List
- Java Program for Recursive Bubble Sort
- Bubble Sort for Linked List by Swapping nodes
- C program for Time Complexity plot of Bubble, Insertion and Selection Sort using Gnuplot
- Queue using Stacks
- Check if the two given stacks are same
- Sorting array using Stacks
- Implement two stacks in an array
- Find maximum sum possible equal sum of three stacks
Improved By : 29AjayKumar