Concept
Merge sort splits the array in half, sorts each half, and merges the two sorted halves. It is O(n log n) in every case and stable, at the cost of O(n) extra memory.
The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.
A few lines that show Merge sort doing its job.
def merge_sort(a):
if len(a) <= 1:
return a
mid = len(a) // 2
left, right = merge_sort(a[:mid]), merge_sort(a[mid:])
merged, i, j = [], 0, 0
while i < len(left) and j < len(right):
if left[i] <= right[j]:
merged.append(left[i]); i += 1
else:
merged.append(right[j]); j += 1
return merged + left[i:] + right[j:]Using < instead of <= when merging. Equal elements from the right half then come before those from the left, and the sort is no longer stable.
Your roadmap puts these before Merge sort, unless you already know them.
Once a session shows you understand Merge sort, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.
Topics that build on Merge sort.