Merge Sort
- Ω( n log n )
- Θ( n log n )
- O( n log n )
- space O( n )
Array
Comparator strip
Index down, time to the right
- Comparisons count only element-against-element checks; loop and index checks are never counted.
- Reads and writes include reads and writes to helper buffers (merge, counting, radix, bucket and tim sort).
- A swap counts once, as a swap. It is never also counted as a read or a write.
- Real time is the average time of the counted sort on this device over repeated runs, measured once before playback and independent of animation speed. Visual time is the time spent animating, excluding pauses.
How merge sort works
Merge sort is a divide and conquer algorithm. It splits the array in half, sorts each half recursively, and merges the two sorted halves by repeatedly taking the smaller of the two front elements.
Read more
Its running time is O(n log n) in every case and it is stable, but merging needs an auxiliary buffer of the same size as the array. Reads and writes to that buffer are counted separately from the main array.
| Best time | Ω( n log n ) | good |
|---|---|---|
| Average time | Θ( n log n ) | good |
| Worst time | O( n log n ) | good |
| Worst space | O( n ) | fair |
Big O notation gives the lower, average and upper bounds of an algorithm. Time complexity bounds the number of operations; space complexity bounds the extra memory it uses.