Course Solutions Uncategorized (Solved) : Merge Sort Time Complexity Nlogn Two Unsorted Stacks Wanted Merge One Final Sorted Stack T Q35375407 . . . .

(Solved) : Merge Sort Time Complexity Nlogn Two Unsorted Stacks Wanted Merge One Final Sorted Stack T Q35375407 . . . .

 

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

OR

PayPal Gateway not configured

OR

PayPal Gateway not configured

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post