Loading…
Loading…
Charting your lesson…
Opening your lesson…
Merge Sort and Divide-and-Conquer
Lesson 2 of 7
6 steps•about 11 min•11 flashcards
Step 1/ 61 min
Merge sort is the reliable O(n log n) sort — guaranteed, stable, and the template for divide-and-conquer thinking. It also powers counting inversions and external sorting of data too big for memory.
Core idea: divide the array in half, conquer by recursively sorting each half, then combine by merging two sorted halves into one. Merging two sorted arrays of total size n is a linear scan, and the recursion has log n levels, giving O(n log n).
Step 2/ 61 min
def mergeSort(a):
if len(a) <= 1: return a
mid = len(a)//2
left = mergeSort(a[:mid])
right = mergeSort(a[mid:])
return merge(left, right) # two-pointer merge of sorted halves
The merge walks both halves with two pointers, always taking the smaller front element → O(n) per level.
The recurrence is T(n) = 2·T(n/2) + O(n). By the Master Theorem this is O(n log n) in all cases (best, average, worst) — merge sort never degrades. The price is O(n) auxiliary space for the merge buffer (arrays), though on linked lists it can be O(1) auxiliary and O(log n) stack.
Step 3/ 61 min
Merge sort [38, 27, 43, 3]. Split → [38,27] and [43,3]. Sort halves → [27,38] and [3,43]. Merge with two pointers: 3<27 → 3; 27<43 → 27; 38<43 → 38; then 43. Result [3,27,38,43]. Two levels of splitting, each merge O(n), total O(n log n).
Step 4/ 61 min
Step 5/ 61 min
Claiming merge sort is in-place. The standard array version needs O(n) auxiliary space for merging. If an interviewer asks for O(1) extra space, merge sort is not the answer (consider heapsort, or an in-place merge which is complex and slow).
Step 6/ 6· Worked examples1 min
Problem
Sort [38, 27, 43, 3] using merge sort, showing the divide and merge steps.
Try it on paper first.
Recall
Keep this
Usage: recognise the 2T(n/2)+n pattern as n log n instantly.
Merge sort is the reliable O(n log n) sort: it guarantees that bound in every case, is stable, and is the model divide-and-conquer algorithm. Interviewers use it to test recursion, the merge step, and its role in counting inversions and sorting linked lists.
Divide the array in half, recursively sort each half, then merge the two sorted halves in linear time with two pointers. The recurrence T(n) = 2T(n/2) + O(n) solves to O(n log n) by the Master Theorem. It needs O(n) auxiliary space for the merge (O(1) for linked lists via pointer splicing) and is stable. The merge step also counts "split inversions", giving an O(n log n) inversion-count algorithm.
| Aspect | Value |
|---|---|
| Time (all cases) | O(n log n) |
| Space (array) | O(n) |
| Space (linked list) | O(1)/O(log n) |
| Stable | Yes |
| Recurrence | 2T(n/2) + O(n) |
Sort an array/list, count inversions, "count of smaller numbers after self", or external sort reasoning. Interviewers expect the divide-merge structure, the O(n log n) guarantee, and stability.
Merge sort divides, recursively sorts, and merges in O(n) — guaranteed O(n log n), stable, O(n) space (O(1) for lists). It's the model divide-and-conquer and the basis for O(n log n) inversion counting and external sorting.
You've read 0 of 6 steps
Now lock it in — questions straight after reading are what make it stick.