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 - 顯示更新後的陣列,列印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] < 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環境中使用降序方式對陣列元素進行排序有了廣泛的瞭解。

更新於: 2023年4月12日

3K+ 瀏覽量

開啟您的職業生涯

透過完成課程獲得認證

開始學習
廣告