Divide · Conquer · Merge · O(n log n)
mergeSort(left) and mergeSort(right). Bubble sort and insertion sort don't do this.mid = LEN DIV 2 and slicing into two halves. The function calls merge on the two recursively-sorted halves.left[0] with right[0] — always the first (smallest) element of each sub-list.result + left + right at the end — whichever sub-list isn't empty gets appended wholesale.| BUBBLE SORT | MERGE SORT | |
|---|---|---|
| Best case | O(n)* | O(n log n) |
| Average case | O(n²) | O(n log n) |
| Worst case | O(n²) | O(n log n) |
| Memory | In-place | Extra space needed |
| Simplicity | Simple | More complex |
| Large data | Very slow | Fast |
Next up: 2.1.3e — Insertion Sort