Permalink
Browse files

Moved insertion sort, updated merge sort recursive

  • Loading branch information...
1 parent 9528a06 commit 3f7b8faaa57871c5b9ed05ba8353d994c5f07e91 @nzakas committed Sep 18, 2012
@@ -29,17 +29,19 @@
* @return {Array} The merged array.
*/
function merge(left, right){
- var result = [];
+ var result = [],
+ il = 0,
+ ir = 0;
- while (left.length > 0 && right.length > 0){
- if (left[0] < right[0]){
- result.push(left.shift());
+ while (il < left.length && ir < right.length){
+ if (left[il] < right[ir]){
+ result.push(left[il++]);
} else {
- result.push(right.shift());
+ result.push(right[ir++]);
}
}
- return result.concat(left).concat(right);
+ return result.concat(left.slice(il)).concat(right.slice(ir));
}
/**
@@ -50,7 +52,7 @@ function merge(left, right){
*/
function mergeSort(items){
- if (items.length == 1) {
+ if (items.length < 2) {
return items;
}

0 comments on commit 3f7b8fa

Please sign in to comment.