# Bubble sort using two Stacks

Prerequisite : Bubble Sort
Write a function that sort an array of integers using stacks and also uses bubble sort paradigm.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Algorithm:

```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.
```

## Java

 `// Java program for bubble sort ` `// using stack ` ` `  `import` `java.util.Arrays; ` `import` `java.util.Stack; ` ` `  `public` `class` `Test  ` `{ ` `    ``// Method for bubble sort using Stack ` `    ``static` `void` `bubbleSortStack(``int` `arr[], ``int` `n) ` `    ``{ ` `        ``Stack s1 = ``new` `Stack<>(); ` `         `  `        ``// Push all elements of array in 1st stack ` `        ``for` `(``int` `num : arr) ` `            ``s1.push(num);        ` `         `  `        ``Stack s2 = ``new` `Stack<>(); ` `         `  `        ``for` `(``int` `i = ``0``; i < n; i++)  ` `        ``{ ` `            ``// alternatively ` `            ``if` `(i % ``2` `== ``0``) ` `            ``{ ` `                ``while` `(!s1.isEmpty()) ` `                ``{ ` `                    ``int` `t = s1.pop(); ` `                     `  `                    ``if` `(s2.isEmpty()) ` `                        ``s2.push(t);                     ` `                    ``else`  `                    ``{ ` `                        ``if` `(s2.peek() > t) ` `                        ``{ ` `                            ``// swapping ` `                            ``int` `temp = s2.pop(); ` `                            ``s2.push(t); ` `                            ``s2.push(temp); ` `                        ``} ` `                        ``else` `                        ``{ ` `                            ``s2.push(t); ` `                        ``} ` `                    ``} ` `                ``} ` `                 `  `                ``// tricky step ` `                ``arr[n-``1``-i] = s2.pop(); ` `            ``}             ` `            ``else` `            ``{ ` `                ``while``(!s2.isEmpty()) ` `                ``{ ` `                    ``int` `t = s2.pop(); ` `                     `  `                    ``if` `(s1.isEmpty()) ` `                        ``s1.push(t); ` `                     `  `                    ``else`  `                    ``{ ` `                        ``if` `(s1.peek() > t) ` `                        ``{ ` `                            ``// swapping ` `                            ``int` `temp = s1.pop(); ` `                             `  `                            ``s1.push(t); ` `                            ``s1.push(temp); ` `                        ``} ` `                        ``else` `                            ``s1.push(t); ` `                    ``} ` `                ``} ` `                 `  `                ``// tricky step ` `                ``arr[n-``1``-i] = s1.pop(); ` `            ``} ` `        ``}         ` `        ``System.out.println(Arrays.toString(arr)); ` `    ``} ` `     `  `    ``// Driver Method ` `    ``public` `static` `void` `main(String[] args)  ` `    ``{ ` `        ``int` `arr[] = {``15``, ``12``, ``44``, ``2``, ``5``, ``10``};        ` `        ``bubbleSortStack(arr, arr.length); ` `    ``} ` `} `

## C#

 `// C# program for bubble sort ` `// using stack ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GFG  ` `{ ` `     `  `// Method for bubble sort using Stack ` `static` `void` `bubbleSortStack(``int` `[]arr, ``int` `n) ` `{ ` `    ``Stack<``int``> s1 = ``new` `Stack<``int``>(); ` `     `  `    ``// Push all elements of array in 1st stack ` `    ``foreach` `(``int` `num ``in` `arr) ` `        ``s1.Push(num);      ` `     `  `    ``Stack<``int``> s2 = ``new` `Stack<``int``>(); ` `     `  `    ``for` `(``int` `i = 0; i < n; i++)  ` `    ``{ ` `        ``// alternatively ` `        ``if` `(i % 2 == 0) ` `        ``{ ` `            ``while` `(s1.Count != 0) ` `            ``{ ` `                ``int` `t = s1.Pop(); ` `                 `  `                ``if` `(s2.Count == 0) ` `                    ``s2.Push(t);                  ` `                ``else` `                ``{ ` `                    ``if` `(s2.Peek() > t) ` `                    ``{ ` `                        ``// swapping ` `                        ``int` `temp = s2.Pop(); ` `                        ``s2.Push(t); ` `                        ``s2.Push(temp); ` `                    ``} ` `                    ``else` `                    ``{ ` `                        ``s2.Push(t); ` `                    ``} ` `                ``} ` `            ``} ` `             `  `            ``// tricky step ` `            ``arr[n - 1 - i] = s2.Pop(); ` `        ``}          ` `        ``else` `        ``{ ` `            ``while``(s2.Count != 0) ` `            ``{ ` `                ``int` `t = s2.Pop(); ` `                 `  `                ``if` `(s1.Count == 0) ` `                    ``s1.Push(t); ` `                 `  `                ``else` `                ``{ ` `                    ``if` `(s1.Peek() > t) ` `                    ``{ ` `                        ``// swapping ` `                        ``int` `temp = s1.Pop(); ` `                         `  `                        ``s1.Push(t); ` `                        ``s1.Push(temp); ` `                    ``} ` `                    ``else` `                        ``s1.Push(t); ` `                ``} ` `            ``} ` `             `  `            ``// tricky step ` `            ``arr[n - 1 - i] = s1.Pop(); ` `        ``} ` `    ``}      ` `    ``Console.WriteLine(``"["` `+ String.Join(``", "``, arr) + ``"]"``); ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main(String[] args)  ` `{ ` `    ``int` `[]arr = {15, 12, 44, 2, 5, 10};      ` `    ``bubbleSortStack(arr, arr.Length); ` `} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

Output:

```[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 contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

My Personal Notes arrow_drop_up

Improved By : 29AjayKumar

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.