
This lesson is part of the Merge Sort series. Merge sort divides work into independent parts, which creates an opportunity for parallel execution. Parallelism adds coordination costs, so it helps only when the work is large enough.

Follow the animation step by step
The animation sorts [8, 3, 5, 1] and highlights where independent work can happen concurrently.
- Begin with one range: the input is [8, 3, 5, 1].
- Divide into independent halves: split it into [8, 3] and [5, 1]. Neither half needs data from the other while it is being sorted.
- Sort both halves at the same time: separate workers can sort them concurrently, producing [3, 8] and [1, 5]. For a small example, task setup can cost more than the saved time.
- Join the workers: wait until both halves are sorted.
- Merge their results: compare the two sorted runs and write [1, 3, 5, 8]. Parallel sorting still needs this coordination point before the final result is complete.

Parallelize the recursive calls
After dividing an array into two halves, sort the halves at the same time on separate workers. Then merge the results. The two recursive calls are independent because they work on separate ranges or buffers.
parallelMergeSort(values):
if range is small:
use a sequential sort
divide range into left and right
start sorting left and right concurrently
wait for both results
merge the two sorted rangesFor small arrays, starting tasks can cost more than the saved time. Real implementations use a threshold: below it, sort sequentially. They also limit the number of active tasks to avoid overwhelming the machine.
Parallelize the merge
A large merge can also be divided. Choose a pivot in one sorted half, use binary search to find its position in the other half, and place the pivot at its final output position. The elements on either side form smaller merge problems that can run concurrently.
Work distribution matters. If one side gets most of the values, workers finish at different times and some processors sit idle. Good partitioning aims for balanced output ranges. Parallel merge can improve throughput, but requires careful indexing and separate output regions to prevent races.
Work, span, and practical limits
The total work for merge sort stays O(n log n). With enough processors, the ideal critical path can approach O(log² n) for recursive sorting with parallel merge, though practical performance depends on the merge method, hardware, memory bandwidth, and task scheduling. This ideal bound is not a promise of a matching speedup on a real machine.
Parallel sorting is most useful when datasets are large and the system has spare cores. Benchmark realistic data sizes and include the cost of task creation, memory allocation, and copying. Compare the parallel implementation with a tuned sequential baseline before choosing it.


