Merging and Sorting Two Unsorted Stacks
Last Updated :
17 Aug, 2022
Given 2 input stacks with elements in an unsorted manner. Problem is to merge them into a new final stack, such that the elements become arranged in a sorted manner.
Examples:
Input : s1 : 9 4 2 1
s2: 8 17 3 10
Output : final stack: 1 2 3 4 8 9 10 17
Input : s1 : 5 7 2 6 4
s2 : 12 9 3
Output : final stack: 2 3 4 5 6 7 9 12
Create an empty stack to store result. We first insert elements of both stacks into the result. Then we sort the result stack.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
stack< int > sortStack(stack< int >& input)
{
stack< int > tmpStack;
while (!input.empty()) {
int tmp = input.top();
input.pop();
while (!tmpStack.empty() && tmpStack.top() > tmp) {
input.push(tmpStack.top());
tmpStack.pop();
}
tmpStack.push(tmp);
}
return tmpStack;
}
stack< int > sortedMerge(stack< int >& s1, stack< int >& s2)
{
stack< int > res;
while (!s1.empty()) {
res.push(s1.top());
s1.pop();
}
while (!s2.empty()) {
res.push(s2.top());
s2.pop();
}
return sortStack(res);
}
int main()
{
stack< int > s1, s2;
s1.push(34);
s1.push(3);
s1.push(31);
s2.push(1);
s2.push(12);
s2.push(23);
stack< int > tmpStack = sortedMerge(s1, s2);
cout << "Sorted and merged stack :\n" ;
while (!tmpStack.empty()) {
cout << tmpStack.top() << " " ;
tmpStack.pop();
}
}
|
Java
import java.io.*;
import java.util.*;
public class GFG {
static Stack<Integer> res = new Stack<Integer>();
static Stack<Integer> tmpStack = new Stack<Integer>();
static void sortStack(Stack<Integer> input)
{
while (input.size() != 0 )
{
int tmp = input.peek();
input.pop();
while (tmpStack.size() != 0 &&
tmpStack.peek() > tmp)
{
input.push(tmpStack.peek());
tmpStack.pop();
}
tmpStack.push(tmp);
}
}
static void sortedMerge(Stack<Integer> s1,
Stack<Integer> s2)
{
while (s1.size() != 0 ) {
res.push(s1.peek());
s1.pop();
}
while (s2.size() != 0 ) {
res.push(s2.peek());
s2.pop();
}
sortStack(res);
}
public static void main(String args[])
{
Stack<Integer> s1 = new Stack<Integer>();
Stack<Integer> s2 = new Stack<Integer>();
s1.push( 34 );
s1.push( 3 );
s1.push( 31 );
s2.push( 1 );
s2.push( 12 );
s2.push( 23 );
sortedMerge(s1, s2);
System.out.println( "Sorted and merged stack :" );
while (tmpStack.size() != 0 ) {
System.out.print(tmpStack.peek() + " " );
tmpStack.pop();
}
}
}
|
Python3
def sortStack( Input ):
tmpStack = []
while len ( Input ) ! = 0 :
tmp = Input [ - 1 ]
Input .pop()
while len (tmpStack) ! = 0 and tmpStack[ - 1 ] > tmp:
Input .append(tmpStack[ - 1 ])
tmpStack.pop()
tmpStack.append(tmp)
return tmpStack
def sortedMerge(s1, s2):
res = []
while len (s1) ! = 0 :
res.append(s1[ - 1 ])
s1.pop()
while len (s2) ! = 0 :
res.append(s2[ - 1 ])
s2.pop()
return sortStack(res)
s1 = []
s2 = []
s1.append( 34 )
s1.append( 3 )
s1.append( 31 )
s2.append( 1 )
s2.append( 12 )
s2.append( 23 )
tmpStack = []
tmpStack = sortedMerge(s1, s2)
print ( "Sorted and merged stack :" )
while len (tmpStack) ! = 0 :
print (tmpStack[ - 1 ], end = " " )
tmpStack.pop()
|
C#
using System;
using System.Collections.Generic;
class GFG {
static Stack< int > sortStack( ref Stack< int > input)
{
Stack< int > tmpStack = new Stack< int >();
while (input.Count != 0)
{
int tmp = input.Peek();
input.Pop();
while (tmpStack.Count != 0 &&
tmpStack.Peek() > tmp)
{
input.Push(tmpStack.Peek());
tmpStack.Pop();
}
tmpStack.Push(tmp);
}
return tmpStack;
}
static Stack< int > sortedMerge( ref Stack< int > s1,
ref Stack< int > s2)
{
Stack< int > res = new Stack< int >();
while (s1.Count!=0) {
res.Push(s1.Peek());
s1.Pop();
}
while (s2.Count!=0) {
res.Push(s2.Peek());
s2.Pop();
}
return sortStack( ref res);
}
static void Main()
{
Stack< int > s1 = new Stack< int >();
Stack< int > s2 = new Stack< int >();
s1.Push(34);
s1.Push(3);
s1.Push(31);
s2.Push(1);
s2.Push(12);
s2.Push(23);
Stack< int > tmpStack = new Stack< int >();
tmpStack = sortedMerge( ref s1, ref s2);
Console.Write( "Sorted and merged stack :\n" );
while (tmpStack.Count!=0) {
Console.Write(tmpStack.Peek() + " " );
tmpStack.Pop();
}
}
}
|
Javascript
<script>
let res = [];
let tmpStack = [];
function sortStack(input)
{
while (input.length != 0)
{
let tmp = input[input.length - 1];
input.pop();
while (tmpStack.length != 0 &&
tmpStack[tmpStack.length - 1] > tmp)
{
input.push(tmpStack[tmpStack.length - 1]);
tmpStack.pop();
}
tmpStack.push(tmp);
}
}
function sortedMerge(s1, s2)
{
while (s1.length != 0) {
res.push(s1[s1.length - 1]);
s1.pop();
}
while (s2.length != 0) {
res.push(s2[s2.length - 1]);
s2.pop();
}
sortStack(res);
}
let s1 = [];
let s2 = [];
s1.push(34);
s1.push(3);
s1.push(31);
s2.push(1);
s2.push(12);
s2.push(23);
sortedMerge(s1, s2);
document.write( "Sorted and merged stack :" + "</br>" );
while (tmpStack.length != 0) {
document.write(tmpStack[tmpStack.length - 1] + " " );
tmpStack.pop();
}
</script>
|
Output
Sorted and merged stack :
34 31 23 12 3 1
Complexity Analysis:
- Time Complexity: O((n+m)2) where n and m are number of elements in both the stacks respectively.
- Auxiliary Space: O(n+m)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...