Permalink
Browse files

Fixed small error in algorithm

  • Loading branch information...
1 parent 02a2745 commit ebe08e5abb92b3b48f9e544d2e6750ebbb0bfcd9 Nicholas C. Zakas committed May 30, 2011
Showing with 9 additions and 3 deletions.
  1. +9 −3 algorithms/sorting/merge-sort-iterative/merge-sort-iterative.js
@@ -1,6 +1,6 @@
/*
* Iterative merge sort implementation in JavaScript
- * Copyright (c) 2009 Nicholas C. Zakas
+ * Copyright (c) 2009-2011 Nicholas C. Zakas
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -39,7 +39,13 @@ function merge(left, right){
}
}
- return result.concat(left).concat(right);
+ result = result.concat(left).concat(right);
+
+ //make sure remaining arrays are empty
+ left.splice(0, left.length);
+ right.splice(0, right.length);
+
+ return result;
}
/**
@@ -59,7 +65,7 @@ function mergeSort(items){
}
work.push([]); //in case of odd number of items
- for (var lim=len; lim > 1; lim = (lim+1)/2){
+ for (var lim=len; lim > 1; lim = Math.floor((lim+1)/2)){
for (var j=0,k=0; k < lim; j++, k+=2){
work[j] = merge(work[k], work[k+1]);
}

0 comments on commit ebe08e5

Please sign in to comment.