-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshellsort.c
120 lines (113 loc) · 2.42 KB
/
shellsort.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "shellsort.h"
void shell_sort(data array[], int len) {
int gap = len/2; // gap of comparisons
int i, j;
data tmp; // temporary variable used to swap elements
while (gap > 0) {
for (i = gap; i < len; i++) {
tmp = array[i];
j = i;
while (j >= gap && array[j-gap] > tmp) {
array[j] = array[j-gap];
j -= gap;
}
array[j] = tmp;
}
if (1 < gap && gap < 4)
gap = 1;
else
gap = (int)(gap / 2.2);
}
}
void shell_sort_debug(data array[], int len, STAT *stat) {
int gap = len/2; // gap of comparisons
int i, j;
data tmp; // temporary variable used to swap elements
stat->space++;
stat->allo++;
while (gap > 0) {
for (i = gap; i < len; i++) {
tmp = array[i];
j = i;
while (j >= gap && array[j-gap] > tmp) {
array[j] = array[j-gap];
j -= gap;
stat->comp++;
stat->write++;
}
array[j] = tmp;
stat->write++;
}
if (1 < gap && gap < 4)
gap = 1;
else
gap = (int)(gap / 2.2);
}
}
/*
According to Marcin Ciura, the best gap sequence is 1, 4, 10, 23, 57, 132, 301, 701, 1750 and then a geometric progression with common ratio 2.2
Source: http://en.wikipedia.org/wiki/Shell_sort#Gap_sequence
*/
int next_gap(int gap) {
if (gap <= 1)
return 0;
else if (gap <= 4)
return 1;
else if (gap <= 10)
return 4;
else if (gap <= 23)
return 10;
else if (gap <= 57)
return 23;
else if (gap <= 132)
return 57;
else if (gap <= 301)
return 132;
else if (gap <= 701)
return 301;
else if (gap <= 1750)
return 701;
float newgap = 1750;
while (2.2*newgap < gap)
newgap = (int)(2.2*newgap);
return newgap;
}
void ciura_shell_sort(data array[], int len) {
int gap = next_gap(len/2); // gap of comparisons
int i, j;
data tmp; // temporary variable used to swap elements
while (gap > 0) {
for (i = gap; i < len; i++) {
tmp = array[i];
j = i;
while (j >= gap && array[j-gap] > tmp) {
array[j] = array[j-gap];
j -= gap;
}
array[j] = tmp;
}
gap = next_gap(gap);
}
}
void ciura_shell_sort_debug(data array[], int len, STAT *stat) {
int gap = next_gap(len); // gap of comparisons
int i, j;
data tmp; // temporary variable used to swap elements
stat->space++;
stat->allo++;
while (gap > 0) {
for (i = gap; i < len; i++) {
tmp = array[i];
j = i;
while (j >= gap && array[j-gap] > tmp) {
array[j] = array[j-gap];
j -= gap;
stat->comp++;
stat->write++;
}
array[j] = tmp;
stat->write++;
}
gap = next_gap(gap);
}
}