From the course: Parallel and Concurrent Programming with Java 2

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Solution: Merge sort in Java

Solution: Merge sort in Java - Java Tutorial

From the course: Parallel and Concurrent Programming with Java 2

Start my 1-month free trial

Solution: Merge sort in Java

(music) - [Speaker] For our solution to the merge sort challenge, we used Java's ForkJoin framework to implement the divide and conquer merge sort algorithm. Within the public sort method on line 75, our program creates a ForkJoin pool, with the number of workers based on the number of processors in this computer. And then it calls the invoke method on line 78 to execute a new parallel worker with input indices representing the full length of the array to sort. That parallel worker class is defined as a subclass of recursive action, which is a ForkJoinTask that does not return a result, and it doesn't need to here because this merge sort algorithm sorts the array in place. The parallel worker accomplishes basically the same thing as the second private sort method from the sequential implementation. In its compute method on line 92, it checks to see if the left index is less than the right index, if so, then the sub array needs to be divided further so it calculates the midpoint and…

Contents