Java 8 array parallel sorting example | array partial sorting

Keywords: Java array

On this page, we will provide an example of java 8 Arrays parallel sorting.

Java 8 introduces a new method, parallelSort(), in the array class.

1. Java 8 Arrays.parallelSort() uses the sort merge algorithm to decompose the array into sub arrays, and then sort and merge them themselves.

2. The array is decomposed into subarrays, and the subarray is decomposed into another subarray again until the length of the subarray reaches the minimum granularity.

3. After multiple splits, once the subarrays reach the minimum granularity, they are sorted with Arrays.sort().

4. Arrays.sort() uses the dual pivot quicksort algorithm to sort elements.

5. All parallel tasks used by arrays. Parallelsort() are executed by the ForkJoin common pool.

6. Arrays.parallelSort() sorts the complete array or the elements between the given to and from indexes.

7. If the size of the array is less than the minimum granularity, there will be no parallel processing.

Arrays.parallelSort() method description

1. Find a way to sort the whole array in natural order. These objects must be comparable.

void parallelSort(T[] a)

2. It sorts the elements between the from and to indexes. The object must be comparable.

void parallelSort(T[] a, int fromIndex, int toIndex)

3. It sorts the entire array. Sorting is based on a given comparator object.

void parallelSort(T[] a, Comparator<? super T> cmp)

4. It sorts the elements between the from and to indexes. Sorting is based on a given comparator object.

void parallelSort(T[] a, int fromIndex, int toIndex, Comparator<? super T> cmp)

5. Arrays.parallelSort() is also used for raw data types.

Arrays.parallelSort() is used with comparable objects

ParallelSortWithComparable.java

import java.util.Arrays;
import java.util.function.Consumer;
public class ParallelSortWithComparable {
	public static void main(String[] args) {
		User[] users = User.getUsers();
		System.out.println("--Sort complete array--");
		Arrays.parallelSort(users);
		Consumer<User> printUser = u-> System.out.println(u.getName()+"-"+u.getAge());
		Arrays.stream(users).forEach(printUser);		
		System.out.println("--Sort array from index 1 to 4--");
		users = User.getUsers();
		Arrays.parallelSort(users, 1, 4);
		Arrays.stream(users).forEach(printUser);
	}
} 

User.java

public class User implements Comparable<User> {
	private String name;
	private int age;	
	public User(String name, int age) {
		this.name = name;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public int getAge() {
		return age;
	}
	@Override
	public int compareTo(User user) {
		return name.compareTo(user.name);
	}
	public static User[] getUsers() {
		User[] users = new User[6];
		users[0] = new User ("Ram", 25);
		users[1] = new User ("Shyam", 22);
		users[2] = new User ("Mohan", 21);
		users[3] = new User ("Suresh", 30);
		users[4] = new User ("Ramesh", 20);
		users[5] = new User ("Dinesh", 27);
		return users;
	}
} 

output

--Sort complete array--
Dinesh-27
Mohan-21
Ram-25
Ramesh-20
Shyam-22
Suresh-30
--Sort array from index 1 to 4--
Ram-25
Mohan-21
Shyam-22
Suresh-30
Ramesh-20
Dinesh-27 

Note: in Arrays.parallelSort(users, 1, 4), the sorted indexes are 1, 2, 3, excluding 4.

Arrays.parallelSort() is used in conjunction with a comparator

ParallelSortWithComparator.java

import java.util.Arrays;
import java.util.Comparator;
import java.util.function.Consumer;
public class ParallelSortWithComparator {
	public static void main(String[] args) {
		User[] users = User.getUsers();
		Comparator<User> ageComparator = Comparator.comparing(User::getAge);
		System.out.println("--Sort complete array--");
		Arrays.parallelSort(users, ageComparator);
		Consumer<User> printUser = u-> System.out.println(u.getName()+"-"+u.getAge());
		Arrays.stream(users).forEach(printUser);
		System.out.println("--Sort array from index 1 to 4--");
		users = User.getUsers();
		Arrays.parallelSort(users, 1, 4, ageComparator);
		Arrays.stream(users).forEach(printUser);
	}
} 

output

--Sort complete array--
Ramesh-20
Mohan-21
Shyam-22
Ram-25
Dinesh-27
Suresh-30
--Sort array from index 1 to 4--
Ram-25
Mohan-21
Shyam-22
Suresh-30
Ramesh-20
Dinesh-27 

Note: in Arrays.parallelSort(users, 1, 4, ageComparator), the sorted indexes are 1, 2, 3, excluding 4.

Arrays.parallelSort() is used in conjunction with basic data types

ParallelSortWithPrimitiveDataType.java

import java.util.Arrays;
import java.util.function.DoubleConsumer;
import java.util.function.IntConsumer;
public class ParallelSortWithPrimitiveDataType {
	public static void main(String[] args) {
		int[] num1 = {3, 6, 2, 10, 4, 1, 7};
		System.out.println("--Sort complete Integer array--");
		Arrays.parallelSort(num1);
		IntConsumer  printInt = i -> System.out.print(i+" ");
		Arrays.stream(num1).forEach(printInt);
		System.out.println("\n--Sort Integer array from index 1 to 5--");
		int[] num2 = {3, 6, 2, 10, 4, 1, 7};
		Arrays.parallelSort(num2, 1, 5);
		Arrays.stream(num1).forEach(printInt);		
		
		double[] db1 = {3.5, 1.2, 6.7, 8.9, 0.6, 2.3, 5.5};
		System.out.println("\n--Sort complete Double array--");
		Arrays.parallelSort(db1);
		DoubleConsumer  printDB = d -> System.out.print(d+" ");
		Arrays.stream(db1).forEach(printDB);
		System.out.println("\n--Sort Double array from index 1 to 5--");
		double[] db2 = {3.5, 1.2, 6.7, 8.9, 0.6, 2.3, 5.5};
		Arrays.parallelSort(db2, 1, 5);
		Arrays.stream(db2).forEach(printDB);	
	}
} 

output

--Sort complete Integer array--
1 2 3 4 6 7 10 
--Sort Integer array from index 1 to 5--
1 2 3 4 6 7 10 
--Sort complete Double array--
0.6 1.2 2.3 3.5 5.5 6.7 8.9 
--Sort Double array from index 1 to 5--
3.5 0.6 1.2 6.7 8.9 2.3 5.5  

reference

[1]Java Doc: Class Arrays
[2]Java 8 Arrays Parallel Sort Example

Posted by Royalmike on Sat, 20 Nov 2021 05:07:08 -0800