1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 1 1 3 3 19 19 11 19 3 1 | //3.6 Sort a stack in ascending order, only using stacks var stack = require("../../../lib/dataStructures/stack.js"); var sort = function(toSort) { var sorted = new stack(); while (toSort.length > 0) { var data = toSort.pop(); while ((sorted.length > 0) && (sorted.peek() > data)) { toSort.push(sorted.pop()); } sorted.push(data); } return sorted; }; module.exports = sort; |