Java 对数组元素按降序排序
数组是存储在一些连续内存位置上的相同数据类型的集合。数组是java.until包中的一个类,它提供了预定义的排序方法,使用静态方式并且没有返回值。下面是Arrays.sort()方法的语法:
public static void sort(int[] ar, int from_index, int to_index)
在上面的语法中,我们有:
- ar – 数组名称的简写
-
from_index – 可选参数,用于指定排序的起始位置。
-
to_index – 可选参数,表示元素的索引。
这里是一个示例:
Input :Array = {2, 6, 23, 98, 24, 35, 78}
Output:[98, 78, 35, 24, 23, 6, 2]
Input :Array = {1, 2, 3, 4, 5}
Output:[5, 4, 3, 2, 1]
今天在这篇文章中,我们将学习如何使用Java环境对列表中的数组元素进行排序并以降序重新排列它们。
按降序排序数组元素的算法
这里我们写下了可能的算法,通过这个算法我们可以按降序对数组元素进行排序。
- 步骤 1 – 开始
-
步骤 2 – 设置 temp = 0。
-
步骤 3 – 声明一个数组来存放数据。
-
步骤 4 – 用 arr[] = {5,2,8,7,1} 初始化数组。
-
步骤 5 – 打印”原始数组的元素”
-
步骤 6 – 声明一个临时变量来存储元素进行交换。
-
步骤 7 – 使用两个for循环。
-
步骤 8 – 重复 i<arr.length。
-
步骤 9 – 使用第一个for循环来保存元素并遍历所有元素。
-
步骤 10 – 如果(arr[i]<arr[j]),则
temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
- 步骤 11 – 使用第二个for循环与剩余的元素进行比较。
-
步骤 12 – 打印新行。
-
步骤 13 – 通过比较和交换对元素进行排序。
-
步骤 14 – 使用for(i=0;i<arr.length;i++)迭代。
-
步骤 15 – 显示更新后的数组,作为PRINT arr[i]。
-
步骤 16 – 停止
按降序排序数组元素的语法
import java.util.*;
class Tutorialspoint071001 {
public static void main(String[] args){
// Initializing the array for the process
Integer array[] = { 1, 2, 7, 16, 5,6 };
// Sorting the array in a descending order
Arrays.sort(array, Collections.reverseOrder());
// Printing the elements after the process run
System.out.println(Arrays.toString(array));
}
}
遵循的方法
-
方法一 – Java程序按降序对元素进行排序
-
方法二 – 使用临时函数将Java程序按降序对元素进行排序
-
方法三 – 使用通用逻辑将Java程序按降序对元素进行排序
方法1:Java程序按降序对元素进行排序
在这个Java代码中,我们尝试构建一个将数组元素按降序排序的逻辑过程。
示例1
import java.util.*;
public class tutorialspoint {
public static void main(String[] args){
// Initializing the array for the process run
int array[] = { 1, 2, 3, 7, 5, 16 };
// Sorting the array in ascending order if needed
Arrays.sort(array);
// Reversing the array for the main process
reverse(array);
// Printing the elements from the array
System.out.println(Arrays.toString(array));
}
public static void reverse(int[] array){
// Length of the array is mentioned here
int n = array.length;
// Run the process again. Swapping the first half elements with last half elements
for (int i = 0; i < n / 2; i++) {
// Storing the first half elements in a temp manner
int temp = array[i];
// Assigning the first half to the last half to get result
array[i] = array[n - i - 1];
// Assigning the last half to the first half to get the result
array[n - i - 1] = temp;
}
}
}
输出
[16, 7, 5, 3, 2, 1]
方法2:使用临时函数对元素按降序进行排序的Java程序
在这个Java代码中,我们可以通过使用临时函数来构建一个逻辑,以降序对数组中的元素进行排序。
示例2
import java.util.Scanner;
public class Descendingtutorialspountrddarb{
public static void main(String[] args) {
int n, temp;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. number of elements you want in the array---->:");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter all the elements here now to run the code further ----> :");
for (int i = 0; i < n; i++) {
a[i] = s.nextInt();
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (a[i] < a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
System.out.print("Descending Order Output Is Here. Have A Look!:");
for (int i = 0; i < n - 1; i++) {
System.out.print(a[i] + ",");
}
System.out.print(a[n - 1]);
}
}
输出
Enter no. number of elements you want in the array---->:7
Enter all the elements here now to run the code further ----> :
1
2
3
16
4
5
6
Descending Order Output Is Here. Have A Look!:16,6,5,4,3,2,1
方法3:使用一般逻辑按降序排序元素
在这个Java代码中,我们使用一些常见的函数编写了一个逻辑来按降序排序数组中的元素。
示例3
public class Tutorialspoint {
public static void main(String[] args) {
//Initialize array for the process
int [] arr = new int [] {16, 2022, 2001, 1997, 7};
int temp = 0;
//Displaying elements of an original array to go further
System.out.println("Elements of original array are ---->: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
//Sort the array in descending order. Please go further
for (int i = 0; i < arr.length; i++) {
for (int j = i+1; j < arr.length; j++) {
if(arr[i] < arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
System.out.println();
//Displaying elements of array after sorting process done.
System.out.println("Elements of array sorted in descending order is here ---->: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
输出
Elements of original array are ---->:
16 2022 2001 1997 7
Elements of array sorted in descending order is here ---->:
2022 2001 1997 16 7
结论
我们详细了解了数组元素排序问题。今天我们使用了各种排序方法来解决这个问题,使用了上述提到的语法和算法。希望通过这篇文章,您对如何使用Java环境以降序排序数组元素有了更广泛的了解。