希尔排序完整c语言代码
时间: 2023-12-05 12:41:25 浏览: 79
以下是希尔排序的完整C语言代码:
```c
#include <stdio.h>
void shellSort(int arr[], int n) {
int i, j, gap, temp;
for (gap = n / 2; gap > 0; gap /= 2) {
for (i = gap; i < n; i++) {
temp = arr[i];
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) {
arr[j] = arr[j - gap];
}
arr[j] = temp;
}
}
}
int main() {
int arr[] = {12, 34, 54, 2, 3}, i;
int n = sizeof(arr) / sizeof(arr[0]);
printf("Array before sorting: \n");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
shellSort(arr, n);
printf("\nArray after sorting: \n");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
阅读全文