-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstackSorting.js
More file actions
23 lines (20 loc) · 765 Bytes
/
Copy pathstackSorting.js
File metadata and controls
23 lines (20 loc) · 765 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Stack sorting (tower of Hanoi)
function sortStack(stack) {
let result = [];
while (stack.length > 0) {
// we keep popping the primary stack until empty
let pop = stack.pop();
// in the case of sorting in ascending order, change to
// while (result.length > 0 && result[result.length - 1] > pop)
// While the result is not empty, and the top stack value in the result is greater than the pop value
while (result.length > 0 && result[result.length - 1] < pop) {
// we pop the result and push back into the original stack
let resultPop = result.pop();
stack.push(resultPop);
}
// repeatedly push the correct order into the result
result.push(pop);
}
return result;
}
console.log(sortStack([10, 9, 2, 15, 3, 2, 1, 0]))