You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
26 lines
688 B
26 lines
688 B
8 months ago
|
var swap = require('./swap');
|
||
|
var isSorted = require('./isSorted');
|
||
|
exports = function(arr) {
|
||
|
var cmp =
|
||
|
arguments.length > 1 && arguments[1] !== undefined
|
||
|
? arguments[1]
|
||
|
: isSorted.defComparator;
|
||
|
var len = arr.length;
|
||
|
var gap = Math.floor(len / 2);
|
||
|
while (gap > 0) {
|
||
|
for (var i = gap; i <= len - gap; i++) {
|
||
|
for (var j = i; j > 0; j -= gap) {
|
||
|
if (cmp(arr[j], arr[j - gap]) < 0) {
|
||
|
swap(arr, j, j - gap);
|
||
|
} else {
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
gap = Math.floor(gap / 2);
|
||
|
}
|
||
|
return arr;
|
||
|
};
|
||
|
|
||
|
module.exports = exports;
|