-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShellSort
More file actions
53 lines (51 loc) · 1.11 KB
/
ShellSort
File metadata and controls
53 lines (51 loc) · 1.11 KB
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
#include <stdio.h>
#include <stdlib.h>
int *a;
int fib(long long a){
long long first = 0, second = 1, ans = 0;
while (first + second < a) {
ans = first + second;
first = second;
second = ans;
}
return ans;
}
int compare(unsigned long i, unsigned long j){
if (a[i] < a[j]) return -1;
if (a[i] == a[j]) return 0;
return 1;
}
void swap(unsigned long i, unsigned long j){
int save =a[i];
a[i] = a[j];
a[j] = save;
}
void shellsort(unsigned long nel,
int (*compare)(unsigned long i, unsigned long j),
void (*swap)(unsigned long i, unsigned long j))
{
long long d = fib(nel);
while (d>=1){
int i = d;
while (i < nel){
int loc = i;
while (loc-d >=0 && compare(loc-d, loc) > 0) {
swap(loc-d,loc);
loc-=d;
}
i++;
}
d = fib(d);
}
}
int main(){
int i, n;
scanf("%d", &n);
a = (int*)malloc(n * sizeof(int));
for (i = 0; i < n; i++) scanf("%d", a+i);
shellsort(n, compare, swap);
for (i = 0; i < n; i++) printf("%d ", a[i]);
printf("\n");
free(a);
return 0;
}