#include <stdio.h>

void swap(int* a, int* b) { int temp = *a; *a = *b; *b = temp; }

int partition(int arr[], int low, int high) { int pivot = arr[high]; int i = (low - 1);

for (int j = low; j <= high - 1; j++) {
    if (arr[j] >= pivot) {
        i++;
        swap(&arr[i], &arr[j]);
    }
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);

}

void quickSort(int arr[], int low, int high) { if (low < high) { int pi = partition(arr, low, high);

    quickSort(arr, low, pi - 1);
    quickSort(arr, pi + 1, high);
}

}

int main() { int a[10] = {11, 9, 5, 43, 8, 7, 34, 25, 2, 66}; int n = sizeof(a) / sizeof(a[0]);

quickSort(a, 0, n - 1);

printf("Sorted array in descending order: ");
for (int i = 0; i < n; i++) {
    printf("%d ", a[i]);
}
printf("\n");

return 0;

}

Sorted array in descending order: 66 43 34 25 11 9 8 7 5 2

C语言快速排序算法实现数组降序排序 - 代码示例

原文地址: https://www.cveoy.top/t/topic/qurv 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录