| Stable? | Time | Space |
|---|---|---|
| Yes | O(n log(n)) | O(n) |
Explanation
Split the list into two halves, then keep splitting each half until you end up with single-element lists. Treat each single element as already sorted, then merge pairs back together by comparing the front elements of each list and taking the smaller one first. Keep merging the sorted pieces into bigger sorted lists until everything is merged into one fully sorted list.
- Parallelization: Merge sort is parallel-friendly because, after splitting, the two halves can be sorted independently and concurrently across multiple CPU cores.
- Predictable: Merge sort consistently performs well regardless of input order, providing reliable worst-case performance for large datasets.
Implementation
Merge Sort