Java程序以降序对数组元素进行排序

2023年 8月 28日 41.7k 0

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 步 - 设置温度 =0。

  • 第 3 步 - 声明一个数组来放置数据。

  • 第 4 步 - 使用 arr[] ={5, 2, 8, 7, 1 } 初始化数组。

  • 第 5 步 - 打印“原始数组的元素”

  • 第 6 步 - 声明一个临时变量来在交换时存储元素。

  • 第 7 步 - 使用两个 for 循环来实现相同的目的。

  • 第 8 步 - 重复 i

  • 第 9 步 - 使用第一个 for 循环保存元素并遍历所有元素。

  • 第 10 步 - if(arr[i]

    临时= arr[i]

    arr[i]=arr[j]

    arr[j]=temp

  • 第 11 步 - 使用第二个 for 循环与其余元素进行比较

  • 第 12 步 - 打印新行。

  • 第 13 步 - 通过比较和交换对元素进行排序。

  • 第 14 步 - 使用 for(i=0;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));
}
}

登录后复制

遵循的方法

  • 方法 1 - 按降序对元素进行排序的 Java 程序

  • 方法 2 - 使用 temp 函数按降序对元素进行排序的 Java 程序

  • 方法 3 - 使用通用逻辑对元素进行降序排序的 Java 程序

按降序对元素进行排序的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]

登录后复制

使用 temp 函数按降序对元素进行排序的 Java 程序

在这段 Java 代码中,我们可以使用 temp 函数构建一个逻辑,以降序方式对数组中的元素进行排序。

示例 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

登录后复制

使用通用逻辑对元素进行降序排序

在这段 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] : ");
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 环境对数组元素进行降序排序有一个广泛的了解。

以上就是Java程序以降序对数组元素进行排序的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论