BUPT-homework/semester3/pset5/7-2-Shell-Sort.c

43 lines
684 B
C
Raw Normal View History

2022-11-13 21:16:43 +08:00
#include <stdio.h>
void print(int *arr, int N) {
for (int i = 0; i < N; i++) {
printf("%d,", arr[i]);
}
printf("\n");
}
void shellSort(int *arr, int N) {
int j, key;
for (int gap = N / 2; gap > 0; gap /= 2) {
// insertion sort, with gaps
for (int i = gap; i < N; i++) {
key = arr[i];
j = i;
// remember to compare with gaps
while (j >= gap && arr[j - gap] < key) {
arr[j] = arr[j - gap];
j -= gap;
}
arr[j] = key;
}
print(arr, N);
}
}
int main(void) {
int N;
scanf("%d", &N);
int arr[N];
for (int i = 0; i < N; i++) {
scanf("%d,", &arr[i]);
}
shellSort(arr, N);
return 0;
}