File tree Expand file tree Collapse file tree 1 file changed +9
-3
lines changed
algorithms/sorting/merge-sort-iterative Expand file tree Collapse file tree 1 file changed +9
-3
lines changed Original file line number Diff line number Diff line change 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 }
You can’t perform that action at this time.
0 commit comments