File tree 1 file changed +9
-3
lines changed
algorithms/sorting/merge-sort-iterative
1 file changed +9
-3
lines changed Original file line number Diff line number Diff line change 1
1
/*
2
2
* Iterative merge sort implementation in JavaScript
3
- * Copyright (c) 2009 Nicholas C. Zakas
3
+ * Copyright (c) 2009-2011 Nicholas C. Zakas
4
4
*
5
5
* Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
* of this software and associated documentation files (the "Software"), to deal
@@ -39,7 +39,13 @@ function merge(left, right){
39
39
}
40
40
}
41
41
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 ;
43
49
}
44
50
45
51
/**
@@ -59,7 +65,7 @@ function mergeSort(items){
59
65
}
60
66
work . push ( [ ] ) ; //in case of odd number of items
61
67
62
- for ( var lim = len ; lim > 1 ; lim = ( lim + 1 ) / 2 ) {
68
+ for ( var lim = len ; lim > 1 ; lim = Math . floor ( ( lim + 1 ) / 2 ) ) {
63
69
for ( var j = 0 , k = 0 ; k < lim ; j ++ , k += 2 ) {
64
70
work [ j ] = merge ( work [ k ] , work [ k + 1 ] ) ;
65
71
}
You can’t perform that action at this time.
0 commit comments