I have a single-thread version of the merge sort http://pastebin.com/2uMGjTxr
It creates an array, fills it with random numbers and calles the sort method on it that performs the merge sort:
private static int[] sort(int[] array) {
//TODO: use multiple threads to speed up the sorting
return mergeSort(array, 0, array.length);
}
Now I want to increase performance using the multithreading technique in java. The code is from my tutor and he said I have to add something to the sort method but that actually confuses me.
The merge sort is a devide and conquer algorithm:
- If the list is of length 0 or 1, then it is already sorted. Otherwise:
- Divide the unsorted list into two sublists of about half the size.
- Sort each sublist recursively by re-applying the merge sort.
- Merge the two sublists back into one sorted list.
So I actually would start a thread for each sublist. What do you think? How can merge sort be parallelized according to the give implementation? Has anyone a clue why I should edit the sort method?