-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShellSort.cpp
47 lines (40 loc) · 1.02 KB
/
ShellSort.cpp
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
#include<bits/stdc++.h>
using namespace std;
//Performs a shell sort
void shellSort(int array[], int n) {
int h = 1;
while (h < n) h = 3*h + 1;
while (h > 0)
{
h = h/3;
for (int k = 0; k < h; k++)
{
for (int i = h+k; i<n; i+=h)
{
int key = array[i];
int j = i-h;
while (j>=0 && array[j] > key)
{
array[j+h] = array[j];
j-=h;
}
array[j+h] = key;
//-> invariant: array[0,h,2*h..j] is sorted
}
}
//->invariant: each h-sub-array is sorted
}
}
int main()
{
int a[] = {9,8,7,6,5,4,3,2,1};
int n = sizeof a / sizeof a[0];
printf("Before Shell Sort:\n");
for(int i=0; i<n; i++)
printf("%d ", a[i]);
shellSort(a, n);
printf("\n\nAfter Shell Sort:\n");
for(int i=0; i<n; i++)
printf("%d ", a[i]);
return 0;
}