Lesson 24 — Merge sort
Merge sort: split in half, sort, merge
Merge sort splits the list in half, sorts each half recursively, then merges the two sorted halves (the merge from Lesson 15). It is always O(n log n), and it is stable: equal items keep their original order.
m = len(a) // 2
left, right = a[:m], a[m:]
Why it matters: Python's own sorted() uses Timsort, a merge-sort hybrid. Stability matters when you sort by one column after another.
Write the two recursive calls on the halves. merge() is already done for you.
Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.
A quick 3-question quiz on this lesson — instant feedback.
Ready for more? Optional paid resources that pick up where this lesson leaves off.
Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.
Project-based Algorithms Foundations courses if you want a structured paid path after the free lessons. Wait for a sale — prices drop often.
Browse on Udemy →Search Amazon for Algorithms Foundations books — check the publication year so the examples match current versions.
Search Amazon →