Java 并行数组排序2024 年 8 月 29 日 | 5 分钟阅读 Java 在 Array 类中提供了一个新功能,用于并行排序数组元素。已向 java.util.Arrays 包添加了新方法,这些方法使用 JSR 166 Fork/Join 并行池来并行排序数组。这些方法称为 parallelSort(),并针对所有基本数据类型和可比较对象进行了重载。 下表包含 Arrays 重载的排序方法。 方法 | 描述 |
---|
public static void parallelSort(byte[] a) | 它将指定的数组按升序排列。 | public static void parallelSort(byte[] a, int fromIndex, int toIndex) | 它将数组的指定范围按升序排列。要排序的范围从索引 fromIndex(包括)到索引 toIndex(不包括)。如果 fromIndex == toIndex,则要排序的范围为空。 | public static void parallelSort(char[] a) | 它将指定的数组按升序排列。 | public static void parallelSort(char[] a, int fromIndex, int toIndex) | 它将数组的指定范围按升序排列。要排序的范围从索引 fromIndex(包括)到索引 toIndex(不包括)。如果 fromIndex == toIndex,则要排序的范围为空。 | public static void parallelSort(double[] a) | 它将指定的数组按升序排列。 | public static void parallelSort(double[] a, int fromIndex, int toIndex) | 它将数组的指定范围按升序排列。要排序的范围从索引 fromIndex(包括)到索引 toIndex(不包括)。如果 fromIndex == toIndex,则要排序的范围为空。 | public static void parallelSort(float[] a) | 它将指定的数组按升序排列。 | public static void parallelSort(float[] a, int fromIndex, int toIndex) | 它将数组的指定范围按升序排列。要排序的范围从索引 fromIndex(包括)到索引 toIndex(不包括)。如果 fromIndex == toIndex,则要排序的范围为空。 | public static void parallelSort(int[] a) | 它将指定的数组按升序排列。 | public static void parallelSort(int[] a,int fromIndex, int toIndex) | 它将数组的指定范围按升序排列。要排序的范围从索引 fromIndex(包括)到索引 toIndex(不包括)。如果 fromIndex == toIndex,则要排序的范围为空。 | public static void parallelSort(long[] a) | 它将指定的数组按升序排列。 | public static void parallelSort(long[] a, int fromIndex, int toIndex) | 它将数组的指定范围按升序排列。要排序的范围从索引 fromIndex(包括)到索引 toIndex(不包括)。如果 fromIndex == toIndex,则要排序的范围为空。 | public static void parallelSort(short[] a) | 它将指定的数组按升序排列。 | public static void parallelSort(short[] a,int fromIndex,int toIndex) | 它将数组的指定范围按升序排列。要排序的范围从索引 fromIndex(包括)到索引 toIndex(不包括)。如果 fromIndex == toIndex,则要排序的范围为空。 | public static <T extends Comparable<? super T>> void parallelSort(T[] a) | 根据其元素的自然顺序,将指定的对象数组按升序排序。数组中的所有元素都必须实现 Comparable 接口。此外,数组中的所有元素都必须可以相互比较(即,对于数组中的任何元素 e1 和 e2,e1.compareTo(e2) 不得抛出 ClassCastException)。 | public static <T> void parallelSort(T[] a,Comparator<? super T> cmp) | 它根据指定的比较器所产生的顺序,对指定的对象数组进行排序。数组中的所有元素都必须可以通过指定的比较器进行相互比较(即,c.compare(e1, e2) 不得为数组中的任何元素 e1 和 e2 抛出 ClassCastException)。 | public static <T extends Comparable<? super T>> void parallelSort(T[] a,int fromIndex, int toIndex) | 它根据其元素的自然顺序,对指定的对象数组的指定范围按升序排序。要排序的范围从索引 fromIndex(包括)到索引 toIndex(不包括)。(如果 fromIndex==toIndex,则要排序的范围为空。)此范围中的所有元素都必须实现 Comparable 接口。此外,此范围中的所有元素都必须可以相互比较(即,e1.compareTo(e2) 不得为数组中的任何元素 e1 和 e2 抛出 ClassCastException)。 | public static <T> void parallelSort(T[] a, int fromIndex, int toIndex, Comparator<? super T> cmp) | 它根据指定的比较器所产生的顺序,对指定的对象数组的指定范围进行排序。要排序的范围从索引 fromIndex(包括)到索引 toIndex(不包括)。(如果 fromIndex==toIndex,则要排序的范围为空。)该范围中的所有元素都必须可以通过指定的比较器进行相互比较(即,c.compare(e1, e2) 不得为该范围中的任何元素 e1 和 e2 抛出 ClassCastException)。 |
Java 并行数组排序示例输出 5 8 1 0 6 9
Array elements after sorting
0 1 5 6 8 9
Java 并行数组排序示例:传递开始和结束索引在下面的示例中,我们传递了数组的开始和结束索引。第一个索引是包含的,结束索引是不包含的,即如果我们传递 0 作为开始索引和 4 作为结束索引,则只会对 0 到 3 的索引元素进行排序。 如果开始索引 > 结束索引,则抛出 IllegalArgumentException。 如果开始索引 < 0 或结束索引 > a.length,则抛出 ArrayIndexOutOfBoundsException。 输出 5 8 1 0 6 9 50 -3
Array elements after sorting
0 1 5 8 6 9 50 -3
|