-
Notifications
You must be signed in to change notification settings - Fork 3
/
shell-sort.js
26 lines (26 loc) · 962 Bytes
/
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
/*
* CODE: http://blog.benoitvallon.com/sorting-algorithms-in-javascript/sorting-algorithms-in-javascript-all-the-code/#shell-sort
*
* Shell sort is an in-place comparison sort which can be seen as either a
generalization of sorting by exchange (bubble sort) or sorting by insertion (
insertion sort). The method starts by sorting pairs of elements far apart
from each other, then progressively reducing the gap between elements to be
compared. Starting with far apart elements can move some out-of-place
elements into position faster than a simple nearest neighbor exchange.
*
* MORE INFO: https://en.wikipedia.org/wiki/Shellsort
*/
export function shellSort(arr) {
const len = arr.length;
for (let h = len; h > 0; h = parseInt(h / 2)) {
for (let i = h; i < len; i++) {
let k = arr[i];
let j = i;
for (; j >= h && k < arr[j - h]; j -= h) {
arr[j] = arr[j - h];
}
arr[j] = k;
}
}
return arr;
}