-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShellSort.h
More file actions
35 lines (32 loc) · 795 Bytes
/
ShellSort.h
File metadata and controls
35 lines (32 loc) · 795 Bytes
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
#pragma once
#include <iostream>
using namespace std;
int gaps[] = { 0, 1, 8, 23, 77, 281, 1073, 4193, 16577, 65921, 262913,
1050113, 4197377, 16783361, 67121153};
void ShellSort(unsigned* array, int len) {
int index = 0;
int gap = len / 4;
while (true) {
if (gaps[index] > gap) break;
index++;
}
while (gap > 0) {
//cout << "index=" << index << " gap=" << gap << endl;
for (int i = gap; i < len; i += gap) {
unsigned temp = array[i];
int j = i - gap;
while (j >= 0 && temp < array[j]) {
array[j + gap] = array[j];
//cout << "array[" << j + gap <<"]=" << array[j + gap] << " " << endl;
j -= gap;
}
array[j + gap] = temp;
/*for (int k = 0; k <= 9; k++) {
cout << array[k] << " ";
}
cout << endl;*/
}
index--;
gap = gaps[index];
}
}