-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshellSort.c
61 lines (44 loc) · 958 Bytes
/
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ARRAY_SIZE 10
void printArray(char* name, int arr[], int size) {
printf("%s = {", name);
for (int i=0; i<size; i++) {
printf("%d", arr[i]);
if (i!=size-1)
printf(", ");
}
printf("}\n");
}
void insertionSort2(int arr[], int first, int last, int h) {
int val, pos;
for (int i=first; i<=last; i = i+h) {
val = arr[i];
for (pos=i; pos>first; pos= pos-h) {
if (val<arr[pos-h])
arr[pos]=arr[pos-h];
else
break;
}
arr[pos] = val;
}
}
void shellSort(int arr[], int size) {
int h, i;
for (h=(size/2); h>0; h=(h/2))
for (i=0; i<h; i++) {
insertionSort2(arr, i, size-1, h);
printArray("arr", arr, size);
}
}
void main() {
int arr[ARRAY_SIZE];
srand(time(NULL));
for (int i=0; i<ARRAY_SIZE; i++) {
arr[i] = rand()%1000;
}
printArray("arr", arr, ARRAY_SIZE);
shellSort(arr, ARRAY_SIZE);
printArray("arr", arr, ARRAY_SIZE);
}