给一个数组,如A[]={1,3,5,46,11,22,37,45,44,78,67,98},把此数组按降序用快速排序用c++
时间: 2024-06-13 07:08:26 浏览: 52
以下是C++实现快速排序的代码,可以将给定数组按照降序排列:
```c++
#include<iostream>
using namespace std;
void quickSort(int A[], int left, int right) {
if (left >= right) {
return;
}
int i = left, j = right, pivot = A[left];
while (i < j) {
while (i < j && A[j] <= pivot) {
j--;
}
A[i] = A[j];
while (i < j && A[i] >= pivot) {
i++;
}
A[j] = A[i];
}
A[i] = pivot;
quickSort(A, left, i - 1);
quickSort(A, i + 1, right);
}
int main() {
int A[] = {1, 3, 5, 46, 11, 22, 37, 45, 44, 78, 67, 98};
int n = sizeof(A) / sizeof(A[0]);
quickSort(A, 0, n - 1);
for (int i = n - 1; i >= 0; i--) {
cout << A[i] << " ";
}
return 0;
}
```
阅读全文