Code coverage report for lib/algorithms/3-stacks/sort.js

Statements: 100% (10 / 10)      Branches: 100% (2 / 2)      Functions: 100% (1 / 1)      Lines: 100% (10 / 10)      Ignored: none     

All files » lib/algorithms/3-stacks/ » sort.js
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;