Java Parallel Array Sorting

Java 8 provides a new additional method parallelSort() in the Arrays class of java.util package. It is introduced to support the parallel sorting of array elements. The parallelSort() method uses the concept of multithreading which makes it much faster compared to the normal sort when there are lot of elements.

Java parallel array sorting example

package com.w3schools;

import java.util.Arrays;

public class TestExample {
	public static void main(String args[]){
	//Creating an integer array   
        int[] arr = {15,81,11,20,6,19};  
        //Iterating array elements  
        for (int i : arr) {  
            System.out.print(i+" ");  
        }  
        //Parallel sorting   
        Arrays.parallelSort(arr);  
        System.out.println("\nArray elements after sorting");  
        //Iterating array elements  
        for (int i : arr) {  
            System.out.print(i+" ");  
        }      
	}  
}

Output

15 81 11 20 6 19 
Array elements after sorting
6 11 15 19 20 81 

Java parallel array sorting: Passing Start and End Index

package com.w3schools;

import java.util.Arrays;

public class TestExample {
	public static void main(String args[]){
		//Creating an integer array   
        int[] arr = {15,81,11,20,6,19};  
        //Iterating array elements  
        for (int i : arr) {  
            System.out.print(i+" ");  
        }  
        //Parallel sorting with start, end index 
        Arrays.parallelSort(arr,0,3);   
        System.out.println("\nArray elements after sorting");  
        //Iterating array elements  
        for (int i : arr) {  
            System.out.print(i+" ");  
        }      
	}  
}

Output

15 81 11 20 6 19 
Array elements after sorting
11 15 81 20 6 19