📌  相关文章
📜  Java程序以降序对数组的元素进行排序

📅  最后修改于: 2022-05-13 01:54:23.159000             🧑  作者: Mango

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

按降序对给定数组进行排序,从最大到最小排列元素。排序是系统地排列项目的过程。 sort() 是Java .util.Arrays 中的一个内置函数,用于以优化的复杂性对元素数组进行排序。

例子:

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]

方法#1:

通过将数组和 Collections.reverseOrder() 作为参数传递给 Arrays.sort(),可以按降序对数组元素进行排序。

注意要记住的一件事是,按降序排序时,Arrays.sort() 不接受原始数据类型的数组。

执行:

Java
// Java program to sort the elements in descending order
import java.util.*;
 
class GFG {
    public static void main(String[] args)
    {
 
        // Initializing the array
        Integer array[] = { 1, 2, 3, 4, 5 };
 
        // Sorting the array in descending order
        Arrays.sort(array, Collections.reverseOrder());
 
        // Printing the elements
        System.out.println(Arrays.toString(array));
    }
}


Java
// Java program to sort the elements in descending order
import java.util.*;
 
class GFG {
    public static void main(String[] args)
    {
 
        // Initializing the array
        int array[] = { 1, 2, 3, 4, 5, 6 };
 
        // Sorting the array in ascending order
        Arrays.sort(array);
 
        // Reversing the array
        reverse(array);
 
        // Printing the elements
        System.out.println(Arrays.toString(array));
    }
 
    public static void reverse(int[] array)
    {
 
        // Length of the array
        int n = array.length;
 
        // Swaping the first half elements with last half
        // elements
        for (int i = 0; i < n / 2; i++) {
 
            // Storing the first half elements temporarily
            int temp = array[i];
 
            // Assigning the first half to the last half
            array[i] = array[n - i - 1];
 
            // Assigning the last half to the first half
            array[n - i - 1] = temp;
        }
    }
}


输出
[5, 4, 3, 2, 1]

时间复杂度: O(N logN)

方法#2:

  1. 对给定的数组进行排序。
  2. 反转已排序的数组。

下面是上述方法的实现:

Java

// Java program to sort the elements in descending order
import java.util.*;
 
class GFG {
    public static void main(String[] args)
    {
 
        // Initializing the array
        int array[] = { 1, 2, 3, 4, 5, 6 };
 
        // Sorting the array in ascending order
        Arrays.sort(array);
 
        // Reversing the array
        reverse(array);
 
        // Printing the elements
        System.out.println(Arrays.toString(array));
    }
 
    public static void reverse(int[] array)
    {
 
        // Length of the array
        int n = array.length;
 
        // Swaping the first half elements with last half
        // elements
        for (int i = 0; i < n / 2; i++) {
 
            // Storing the first half elements temporarily
            int temp = array[i];
 
            // Assigning the first half to the last half
            array[i] = array[n - i - 1];
 
            // Assigning the last half to the first half
            array[n - i - 1] = temp;
        }
    }
}
输出
[6, 5, 4, 3, 2, 1]

时间复杂度: O(N logN)