用JAVA。给一个数组,如A[]={1,3,5,46,11,22,37,45,44,78,67,98},用除了冒泡排序外的任意一种方法把此数组按降序排列
时间: 2024-06-13 16:08:22 浏览: 20
以下是使用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);
for (int i : A) {
System.out.print(i + " ");
}
}
public static void quickSortDescending(int[] arr, int low, int high) {
if (low < high) {
int pivot = partition(arr, low, high);
quickSortDescending(arr, low, pivot - 1);
quickSortDescending(arr, pivot + 1, high);
}
}
public static int partition(int[] arr, int low, int high) {
int pivot = arr[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j] >= pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return i + 1;
}
}
```
阅读全文