For merge sort the time complexity is Θ(nlogn),
but what if we had two unsorted stacks and wanted to but mergethem into one final sorted stack! what is the timecomplexity then?
code
/ Java program to merge to unsorted stacks
// into a third stack in sorted way.
import java.io.*;
import java.util.*;
public class GFG {
// This is the temporary stack
static Stack<Integer> res = newStack<Integer>();
static Stack<Integer> tmpStack =new Stack<Integer>();
// Sorts input stack and returns
// sorted stack.
static voidsortStack(Stack<Integer> input)
{
while(input.size() != 0)
{
//pop out the first element
inttmp = input.peek();
input.pop();
//while temporary stack is not empty and
//top of stack is greater than temp
while(tmpStack.size() != 0 &&
tmpStack.peek()> tmp)
{
//pop from temporary stack and push
//it
PayPal Gateway not configured
PayPal Gateway not configured