From the course: Parallel and Concurrent Programming with Java 2
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Solution: Merge sort in Java - Java Tutorial
From the course: Parallel and Concurrent Programming with Java 2
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
-
(Locked)
Welcome to the challenges39s
-
(Locked)
Challenge: Matrix multiply in Java5m 4s
-
(Locked)
Solution: Matrix multiply in Java6m 20s
-
(Locked)
Challenge: Merge sort in Java4m 34s
-
(Locked)
Solution: Merge sort in Java3m 47s
-
(Locked)
Challenge: Download images in Java2m 34s
-
(Locked)
Solution: Download images in Java4m 3s
-
(Locked)
-