给一个数组,如A[]={1,3,5,46,11,22,37,45,44,78,67,98},用除了冒泡排序外的任意一种方法把此数组按降序排列。 java 伪代码
时间: 2024-06-13 11:08:43 浏览: 38
详解C++中的一维数组和二维数组
5星 · 资源好评率100%
以下是使用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 = 0; i < A.length; i++) {
System.out.print(A[i] + " ");
}
}
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;
}
}
```
阅读全文