-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathshell_sort.h
44 lines (41 loc) · 958 Bytes
/
shell_sort.h
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
/*******************************************************************************
* DANIEL'S ALGORITHM IMPLEMENTAIONS
*
* /\ | _ _ ._ o _|_ |_ ._ _ _
* /--\ | (_| (_) | | |_ | | | | | _>
* _|
*
* SHELL SORT
*
* 1. sort array in O(n^(3/2)) time.
*
* https://en.wikipedia.org/wiki/Shellsort
*
******************************************************************************/
#ifndef ALGO_SHELL_SORT_H__
#define ALGO_SHELL_SORT_H__
namespace alg {
/**
* shell sort an array
*/
template<typename T>
static void shell_sort(T *array, int len) {
int h = 1;
while (h < len / 3) {
h = 3 * h + 1; // 1, 4, 13, 40, 121, ...
}
while (h >= 1) {
for (int i = h; i < len; i++) {
int cur = array[i];
int j = i - h;
while (j >= 0 && array[j] > cur) {
array[j + h] = array[j];
j = j - h;
}
array[j + h] = cur;
}
h = h / 3;
}
}
}
#endif //