-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathshell-sort.js
53 lines (48 loc) · 1.18 KB
/
shell-sort.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* Implement [Shell sort]
*
* Copyright (c) 2018, Nima Yazdanmehr.
* Licensed under the MIT License.
*/
'use strict';
function comparator(a, b) {
return a - b;
}
/**
* Bubble sort algorithm.
* Best Case Complexity: O(N*logN).
* Worst Case Complexity: O(N*(logN)^2)
*
* @example
* console.log(shellSort([2, 5, 1, 0, 4])); // [ 0, 1, 2, 4, 5 ]
*
* @public
* @module sorting/shellsort
* @param {Array} array Input array.
* @param {Function} cmp Optional. A function that defines an
* alternative sort order. The function should return a negative,
* zero, or positive value, depending on the arguments.
* @return {Array} Sorted array.
*/
function shellSort(arr, cmp) {
var increment = arr.length / 2;
cmp = cmp || comparator;
while (increment > 0) {
for (let i = increment; i < arr.length; i++) {
var j = i;
var temp = arr[i];
while (cmp(j, increment) >= 0 && cmp(arr[j - increment], temp) > 0) {
arr[j] = arr[j - increment];
j = j - increment;
}
arr[j] = temp;
}
if (increment === 2) {
increment = 1;
} else {
increment = parseInt(increment * 5 / 11);
}
}
return arr;
}
module.exports = shellSort;