-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell_sort.c
48 lines (40 loc) · 1.1 KB
/
shell_sort.c
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
//
// shell_sort.c
// algorithm
//
// Created by jianqing.du on 16-1-25.
// Copyright (c) 2016年. All rights reserved.
//
/*
* implement shell sort in https://en.wikipedia.org/wiki/Shellsort
*/
#include <stdio.h>
#define MAX_ARRAY_SIZE 64
void shell_sort(int array[], int n)
{
//Start with the largest gap and work down to a gap of 1
for (int gap = n / 2; gap > 0; gap /= 2) {
// Do a gapped insertion sort for this gap size.
// The first gap elements a[0..gap-1] are already in gapped order
for (int i = gap; i < n; i++) {
int j = i;
int tmp = array[i];
for (; (j >= gap) && (array[i] < array[j - gap]); j -= gap) {
array[j] = array[j - gap];
}
array[j] = tmp;
}
}
}
int main(int argc, char* argv[])
{
int array[MAX_ARRAY_SIZE];
for (int i = 0; i < MAX_ARRAY_SIZE; i++) {
array[i] = MAX_ARRAY_SIZE - i;
}
shell_sort(array, MAX_ARRAY_SIZE);
for (int i = 0; i < MAX_ARRAY_SIZE; i++) {
printf("%d\n", array[i]);
}
return 0;
}