📄 5_sort_stack.js
/home/palash/git/misc/algorithms/ctci/ch03/5_sort_stack.js
Language: js • Lines: 23
const sortStack = (s) => {
    var s2 = []

    while(s.length !== 0) {
        var temp = s.pop()

        while(s2.length !== 0 && s2[s2.length - 1] < temp) {
            s.push(s2.pop())
        }

        s2.push(temp)
    }

    while(s2.length !== 0) {
        s.push(s2.pop())
    }
}

var s = [7,6,3,4,2,8,9,1,0,3]

console.log(s)
sortStack(s)
console.log(s)