设有如下数组int a10 = 119543873425266;用C语言快速排序函数将数组从大到小排序
以下是使用C语言实现快速排序算法,将数组从大到小排序的代码:
#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
这样就能将数组从大到小排序
原文地址: http://www.cveoy.top/t/topic/iOf0 著作权归作者所有。请勿转载和采集!