📅  最后修改于: 2020-10-13 03:34:07             🧑  作者: Mango
Java在Array类中提供了一个新的附加功能,用于对数组元素进行并行排序。java.util.Arrays包中添加了新方法,该方法使用JSR 166 Fork / Join并行公共池来并行地对数组进行排序。被称为parallelSort(),并且对所有原始数据类型和Comparable对象都进行了重载。
下表包含Arrays重载排序方法。
Methods | Description |
---|---|
public static void parallelSort(byte[] a) | It sorts the specified array into ascending numerical order. |
public static void parallelSort(byte[] a, int fromIndex, int toIndex) | It sorts the specified range of the array into ascending numerical order. The range to be sorted extends from the index fromIndex, inclusive, to the index toIndex, exclusive. If fromIndex == toIndex, the range to be sorted is empty. |
public static void parallelSort(char[] a) | It sorts the specified array into ascending numerical order. |
public static void parallelSort(char[] a, int fromIndex, int toIndex) | It sorts the specified range of the array into ascending numerical order. The range to be sorted extends from the index fromIndex, inclusive, to the index toIndex, exclusive. If fromIndex == toIndex, the range to be sorted is empty. |
public static void parallelSort(double[] a) | It sorts the specified array into ascending numerical order. |
public static void parallelSort(double[] a, int fromIndex, int toIndex) | It sorts the specified range of the array into ascending numerical order. The range to be sorted extends from the index fromIndex, inclusive, to the index toIndex, exclusive. If fromIndex == toIndex, the range to be sorted is empty. |
public static void parallelSort(float[] a) | It sorts the specified array into ascending numerical order. |
public static void parallelSort(float[] a, int fromIndex, int toIndex) | It sorts the specified range of the array into ascending numerical order. The range to be sorted extends from the index fromIndex, inclusive, to the index toIndex, exclusive. If fromIndex == toIndex, the range to be sorted is empty. |
public static void parallelSort(int[] a) | It sorts the specified array into ascending numerical order. |
public static void parallelSort(int[] a,int fromIndex, int toIndex) | It sorts the specified range of the array into ascending numerical order. The range to be sorted extends from the index fromIndex, inclusive, to the index toIndex, exclusive. If fromIndex == toIndex, the range to be sorted is empty. |
public static void parallelSort(long[] a) | It sorts the specified array into ascending numerical order. |
public static void parallelSort(long[] a, int fromIndex, int toIndex) | It sorts the specified range of the array into ascending numerical order. The range to be sorted extends from the index fromIndex, inclusive, to the index toIndex, exclusive. If fromIndex == toIndex, the range to be sorted is empty. |
public static void parallelSort(short[] a) | It sorts the specified array into ascending numerical order. |
public static void parallelSort(short[] a,int fromIndex,int toIndex) | It sorts the specified range of the array into ascending numerical order. The range to be sorted extends from the index fromIndex, inclusive, to the index toIndex, exclusive. If fromIndex == toIndex, the range to be sorted is empty. |
public static |
Sorts the specified array of objects into ascending order, according to the natural ordering of its elements. All elements in the array must implement the Comparable interface. Furthermore, all elements in the array must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the array). |
public static |
It sorts the specified array of objects according to the order induced by the specified comparator. All elements in the array must be mutually comparable by the specified comparator (that is, c.compare(e1, e2) must not throw a ClassCastException for any elements e1 and e2 in the array). |
public static |
It sorts the specified range of the specified array of objects into ascending order, according to the natural ordering of its elements. The range to be sorted extends from index fromIndex, inclusive, to index toIndex, exclusive. (If fromIndex==toIndex, the range to be sorted is empty.) All elements in this range must implement the Comparable interface. Furthermore, all elements in this range must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the array). |
public static |
It sorts the specified range of the specified array of objects according to the order induced by the specified comparator. The range to be sorted extends from index fromIndex, inclusive, to index toIndex, exclusive. (If fromIndex==toIndex, the range to be sorted is empty.) All elements in the range must be mutually comparable by the specified comparator (that is, c.compare(e1, e2) must not throw a ClassCastException for any elements e1 and e2 in the range). |
import java.util.Arrays;
public class ParallelArraySorting {
public static void main(String[] args) {
// Creating an integer array
int[] arr = {5,8,1,0,6,9};
// Iterating array elements
for (int i : arr) {
System.out.print(i+" ");
}
// Sorting array elements parallel
Arrays.parallelSort(arr);
System.out.println("\nArray elements after sorting");
// Iterating array elements
for (int i : arr) {
System.out.print(i+" ");
}
}
}
输出:
5 8 1 0 6 9
Array elements after sorting
0 1 5 6 8 9
在下面的示例中,我们传递数组的开始索引和结束索引。第一个索引为包含索引,结束索引为互斥索引,即,如果我们将0作为起始索引,将4作为终止索引,则将仅对0到3个索引元素进行排序。
如果开始索引>结束索引,则抛出IllegalArgumentException。
如果起始索引<0或结束索引> a.length,则抛出ArrayIndexOutOfBoundsException。
import java.util.Arrays;
public class ParallelArraySorting {
public static void main(String[] args) {
// Creating an integer array
int[] arr = {5,8,1,0,6,9,50,-3};
// Iterating array elements
for (int i : arr) {
System.out.print(i+" ");
}
// Sorting array elements parallel and passing start, end index
Arrays.parallelSort(arr,0,4);
System.out.println("\nArray elements after sorting");
// Iterating array elements
for (int i : arr) {
System.out.print(i+" ");
}
}
}
输出:
5 8 1 0 6 9 50 -3
Array elements after sorting
0 1 5 8 6 9 50 -3