# Java Parallel Array Sorting

Java provides a new additional feature in Array class which is used to sort array elements parallel.New methods has added to java.util.Arrays package that use the JSR 166 Fork/Join parallelism common pool to provide sorting of arrays in parallel.The methods are called parallelSort() and are overloaded for all the primitive data types and Comparable objects.

The following table contains Arrays overloaded sorting methods.

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 <T extends Comparable<? super T>> void parallelSort(T[] a) 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 <T7gt; void parallelSort(T[] a,Comparator<? super T> cmp) 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 <T extends Comparable<? super T>> void parallelSort(T[] a,int fromIndex, int toIndex) 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 <T> void parallelSort(T[] a, int fromIndex, int toIndex, Comparator<? super T> cmp) 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).

### Java Parallel Array Sorting Example

Output:

```5 8 1 0 6 9
Array elements after sorting
0 1 5 6 8 9
```

## Java Parallel Array Sorting Example: Passing Start and End Index

In the following example, we are passing starting and end index of the array. The first index is inclusive and end index is exclusive i.e. if we pass 0 as start index and 4 as end index, only 0 to 3 index elements will be sorted.

It throws IllegalArgumentException if start index > end index.

It throws ArrayIndexOutOfBoundsException if start index < 0 or end index > a.length.

Output:

```5 8 1 0 6 9 50 -3
Array elements after sorting
0 1 5 8 6 9 50 -3
```

### Feedback
{"ft_lang_label":"__label__en","ft_lang_prob":0.5843284,"math_prob":0.7372971,"size":5944,"snap":"2021-31-2021-39","text_gpt3_token_len":1322,"char_repetition_ratio":0.17962962,"word_repetition_ratio":0.6457883,"special_character_ratio":0.21719381,"punctuation_ratio":0.12396694,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96678275,"pos_list":[0,1,2,3,4,5,6],"im_url_duplicate_count":[null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-09-25T04:17:45Z\",\"WARC-Record-ID\":\"<urn:uuid:d65835bf-a4d5-4ac4-8790-d27966d14203>\",\"Content-Length\":\"50199\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:bc525fbe-48a7-4f83-8ed7-69490b98a21a>\",\"WARC-Concurrent-To\":\"<urn:uuid:7a5226d7-a9fd-480b-b90a-a874cb85b61d>\",\"WARC-IP-Address\":\"104.21.79.8\",\"WARC-Target-URI\":\"https://www.javatpoint.com/java-8-parallel-array-sorting\",\"WARC-Payload-Digest\":\"sha1:XWWAGC56EWX73LWRAZY6QBYXETLBBGZQ\",\"WARC-Block-Digest\":\"sha1:PKJG6IQEOR3DOIJGKARVYPDLDYKXEY24\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-39/CC-MAIN-2021-39_segments_1631780057589.14_warc_CC-MAIN-20210925021713-20210925051713-00147.warc.gz\"}"}