SHARE
TWEET

Untitled

a guest Jul 24th, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const sortStack = (stack) => {
  2.   let n = 0;
  3.   sorted = new Stack();
  4.   while (stack.storage.length) {
  5.     tmp = stack.pop();
  6.     n += 1;  
  7.     if (tmp >= sorted.peek()) {
  8.       sorted.push(tmp);
  9.       n += 1;
  10.     } else {
  11.       while (tmp < sorted.peek()) {
  12.         stack.push(sorted.pop());
  13.         n += 1;
  14.       }
  15.       sorted.push(tmp);
  16.       n += 1;
  17.     }
  18.   }
  19.   console.log("n: ", n);
  20.   return sorted;
  21. }
  22.  
  23. sortedStack = sortStack(s);
  24. sortedStack.printContents();
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top