Skip to content

Commit ebe08e5

Browse files
author
Nicholas C. Zakas
committed
Fixed small error in algorithm
1 parent 02a2745 commit ebe08e5

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

algorithms/sorting/merge-sort-iterative/merge-sort-iterative.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Iterative merge sort implementation in JavaScript
3-
* Copyright (c) 2009 Nicholas C. Zakas
3+
* Copyright (c) 2009-2011 Nicholas C. Zakas
44
*
55
* Permission is hereby granted, free of charge, to any person obtaining a copy
66
* of this software and associated documentation files (the "Software"), to deal
@@ -39,7 +39,13 @@ function merge(left, right){
3939
}
4040
}
4141

42-
return result.concat(left).concat(right);
42+
result = result.concat(left).concat(right);
43+
44+
//make sure remaining arrays are empty
45+
left.splice(0, left.length);
46+
right.splice(0, right.length);
47+
48+
return result;
4349
}
4450

4551
/**
@@ -59,7 +65,7 @@ function mergeSort(items){
5965
}
6066
work.push([]); //in case of odd number of items
6167

62-
for (var lim=len; lim > 1; lim = (lim+1)/2){
68+
for (var lim=len; lim > 1; lim = Math.floor((lim+1)/2)){
6369
for (var j=0,k=0; k < lim; j++, k+=2){
6470
work[j] = merge(work[k], work[k+1]);
6571
}

0 commit comments

Comments
 (0)