Java Arrays parallelSort(float[] a) 方法



描述

Java Arrays parallelSort(float[] a) 方法將指定的浮點數陣列按升序排序。此方法使用並行排序合併演算法,將陣列分解成子陣列,對它們進行排序,然後合併以生成排序後的陣列。

宣告

以下是 java.util.Arrays.parallelSort(float[] a) 方法的宣告

public static void parallelSort(float[] a)

引數

a − 要排序的陣列。

返回值

此方法不返回值。

異常

Java Arrays parallelSort​(float[] a, int fromIndex, int toIndex) 方法

描述

Java Arrays parallelSort(float[] a, int fromIndex, int toIndex) 方法將給定浮點數陣列的指定範圍按升序排序。此方法使用並行排序合併演算法,將陣列分解成子陣列,對它們進行排序,然後合併以生成排序後的陣列。

宣告

以下是 java.util.Arrays.parallelSort(float[] a, int fromIndex, int toIndex) 方法的宣告

public static void parallelSort​(float[] a, int fromIndex, int toIndex)

引數

  • a − 要排序的陣列。

  • fromIndex − 包含的第一個要排序的元素的索引。

  • toIndex − 排序的最後一個元素的索引(不包含)。

返回值

此方法不返回任何值。

異常

  • IllegalArgumentException − 如果 fromIndex > toIndex

  • ArrayIndexOutOfBoundsException − 如果 fromIndex < 0 或 toIndex > array.length

浮點數陣列排序示例

以下示例演示了 Java Arrays parallelSort(float[]) 方法的用法。首先,我們建立了一個浮點數陣列,並列印了原始陣列。使用 parallelSort() 方法對陣列進行排序,然後列印排序後的陣列。

package com.tutorialspoint;

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {
      // initialize unsorted array
      float arr[] = { 11.0f, 54.0f, 23.0f, 32.0f, 15.0f, 24.0f, 31.0f, 12.0f };

      System.out.print("Original Array: [");
      // print the array
      for (int i = 0; i < arr.length; i++) {
         System.out.print(arr[i] + " ");
      }
      System.out.println("]");
     
      // sort the array
      Arrays.parallelSort(arr);

      System.out.print("Sorted Array: [");
      // print the array
      for (int i = 0; i < arr.length; i++) {
         System.out.print(arr[i] + " ");
      }
      System.out.println("]");
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Original Array: [11.0 54.0 23.0 32.0 15.0 24.0 31.0 12.0 ]
Sorted Array: [11.0 12.0 15.0 23.0 24.0 31.0 32.0 54.0 ]

指定範圍的浮點數陣列排序示例

以下示例演示了 Java Arrays parallelSort(float[], int, int) 方法的用法。首先,我們建立了一個浮點數陣列,並列印了原始陣列。使用 parallelSort() 方法對陣列進行排序,然後列印排序後的陣列。

package com.tutorialspoint;

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {
      // initialize unsorted array
      float arr[] = { 11.0f, 54.0f, 23.0f, 32.0f, 15.0f, 24.0f, 31.0f, 12.0f };

      System.out.print("Original Array: [");
      // print the array
      for (int i = 0; i < arr.length; i++) {
         System.out.print(arr[i] + " ");
      }
      System.out.println("]");
     
      // sort the array
      Arrays.parallelSort(arr, 0, arr.length);

      System.out.print("Sorted Array: [");
      // print the array
      for (int i = 0; i < arr.length; i++) {
         System.out.print(arr[i] + " ");
      }
      System.out.println("]");
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Original Array: [11.0 54.0 23.0 32.0 15.0 24.0 31.0 12.0 ]
Sorted Array: [11.0 12.0 15.0 23.0 24.0 31.0 32.0 54.0 ]

浮點數子陣列排序示例

以下示例演示了 Java Arrays parallelSort(float[], int, int) 方法的用法。首先,我們建立了一個浮點數陣列,並列印了原始陣列。使用 parallelSort() 方法對子陣列進行排序,然後列印排序後的陣列。

package com.tutorialspoint;

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {
      // initialize unsorted array
      float arr[] = { 11.0f, 54.0f, 23.0f, 32.0f, 15.0f, 24.0f, 31.0f, 12.0f };

      System.out.print("Original Array: [");
      // print the array
      for (int i = 0; i < arr.length; i++) {
         System.out.print(arr[i] + " ");
      }
      System.out.println("]");
     
      // sort first five elements of the array 
      Arrays.parallelSort(arr, 0, 5);

      System.out.print("Sorted Array: [");
      // print the array
      for (int i = 0; i < arr.length; i++) {
         System.out.print(arr[i] + " ");
      }
      System.out.println("]");
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Original Array: [11.0 54.0 23.0 32.0 15.0 24.0 31.0 12.0 ]
Sorted Array: [11.0 15.0 23.0 32.0 54.0 24.0 31.0 12.0 ]
java_util_arrays.htm
廣告