给一个数组,如A[]={1,3,5,46,11,22,37,45,44,78,67,98},用除了冒泡排序外的任意一种方法把此数组按降序排列。用Java实现
时间: 2024-06-13 13:08:23 浏览: 49
以下是使用Java实现使用快速排序算法对给定数组进行降序排列的示例代码:
```java
public class QuickSortDescending {
public static void main(String[] args) {
int[] A = {1, 3, 5, 46, 11, 22, 37, 45, 44, 78, 67, 98};
quickSortDescending(A, 0, A.length - 1);
System.out.println(Arrays.toString(A));
}
public static void quickSortDescending(int[] A, int low, int high) {
if (low < high) {
int pivotIndex = partition(A, low, high);
quickSortDescending(A, low, pivotIndex - 1);
quickSortDescending(A, pivotIndex + 1, high);
}
}
public static int partition(int[] A, int low, int high) {
int pivot = A[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (A[j] >= pivot) {
i++;
int temp = A[i]; A[i] = A[j];
A[j] = temp;
}
}
int temp = A[i + 1];
A[i + 1] = A[high];
A[high] = temp;
return i + 1;
}
}
```
输出结果为:[98, 78, 67, 46, 45, 44, 37, 22, 11, 5, 3, 1]
阅读全文