Java给一个数组,如A[]={1,3,5,46,11,22,37,45,44,78,67,98},用除了冒泡排序外的任意一种方法把此数组按降序排列。
时间: 2024-06-13 15:08:23 浏览: 8
以下是使用Java的Arrays.sort()方法将数组按降序排列的示例代码:
```java
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] A = {1, 3, 5, 46, 11, 22, 37, 45, 44, 78, 67, 98};
Arrays.sort(A);
for (int i = 0; i < A.length / 2; i++) {
int temp = A[i];
A[i] = A[A.length - 1 - i];
A[A.length - 1 - i] = temp;
}
System.out.println(Arrays.toString(A));
}
}
```
在这个示例中,我们首先使用Arrays.sort()方法将数组按升序排列。然后,我们使用一个简单的循环将数组中的元素交换,以便按降序排列。最后,我们使用Arrays.toString()方法将排序后的数组打印到控制台上。
相关问题
用java给一个数组,如A[]={1,3,5,46,11,22,37,45,44,78,67,98},用除了冒泡排序外的任意一种方法把此数组按降序排列。
以下是使用Java中的Arrays.sort()方法将数组按降序排列的例子:
```java
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] A = {1, 3, 5, 46, 11, 22, 37, 45, 44, 78, 67, 98};
Arrays.sort(A);
int[] B = new int[A.length]; for (int i = 0; i < A.length; i++) {
B[i] = A[A.length - i - 1];
}
System.out.println(Arrays.toString(B));
}
}
```
输出结果为:[98, 78, 67, 46, 45, 44, 37, 22, 11, 5, 3, 1]
java给一个数组,如A[]={1,3,5,46,11,22,37,45,44,78,67,98},用除了冒泡排序外的任意一种方法把此数组按降序排列
以下是使用快速排序算法对给定数组按降序排列的Java代码:
```java
public class Main {
public static void main(String[] args) {
int[] A = {1, 3, 5, 46, 11, 22, 37, 45, 44, 78, 67, 98};
quickSort(A, 0, A.length - 1);
for (int i = A.length - 1; i >= 0; i--) {
System.out.print(A[i] + " ");
}
}
public static void quickSort(int[] arr, int left, int right) {
if (left < right) {
int pivotIndex = partition(arr, left, right);
quickSort(arr, left, pivotIndex - 1);
quickSort(arr, pivotIndex + 1, right);
}
}
public static int partition(int[] arr, int left, int right) {
int pivot = arr[left];
int i = left + 1;
int j = right;
while (i <= j) {
if (arr[i] < pivot && arr[j] > pivot) {
swap(arr, i, j);
i++;
j--;
}
if (arr[i] >= pivot) {
i++;
}
if (arr[j] <= pivot) {
j--;
}
}
swap(arr, left, j);
return j;
}
public static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)