希尔排序(C语言)

#include 
void shell_sort(int arr[], int len);
void print_arr(int arr[], int len);

int main(int argc, const char * argv[]) {
    int arr[10] = {5, 2, 6, 0, 3, 9, 1, 7, 4, 8};
    print_arr(arr, 10);
    shell_sort(arr, 10);
    print_arr(arr, 10);
    return 0;
}

void shell_sort(int arr[], int len) {
    int j;
    for (int gap = len/2; gap > 0; gap /= 2) {
        for (int i = gap; i < len; i++) {
            int temp = arr[i];
            for (j = i-gap; j >= 0 && temp < arr[j]; j -= gap) {
                arr[j+gap] = arr[j];
            }
            arr[j+gap] = temp;
        }
    }
}

void print_arr(int arr[], int len) {
    for (int i = 0; i < len; i++) {
        printf("%d ",arr[i]);
    }
    printf("\n");
}

你可能感兴趣的:(希尔排序(C语言))