Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Reddit
Contact us
Hide Buttons

Sort the stack in ascending order

Problem :

Sort the given stack into an ascending order without using recursion.

Logic:

  • Pop an item from the original stack and push it onto the sorted stack.
    • If the poped item from the original stack < the top item in the sorted stack then
    • pop the item from the sorted stack and push it onto the original stack until the sorting order is satisfied.

This post is a follow-up of
Implement stack in Javascript
I recommend reading it first, as the following code uses the methods from it.

Time complexity : O(n^2)

Solution :

You may also like...

Leave a Reply

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