Merge Sort
Your card-sorting algorithm worked one card at a time. Merge sort refuses to sort anything at all — it just splits the list in half, hands each half to another copy of itself, and lets a one-item list count as sorted. That sounds like dodging the work. It's actually one of the fastest sorting algorithms ever devised, and after the Mondrian lab, you already know its secret: it's recursion.
Overview
Concept: Break the list into smaller pieces, sort those pieces, then combine them back together in order.
- Divide: Recursively split the unsorted list into two halves until each sublist contains only one element.
- Conquer: A single-element list is considered sorted.
- Combine (Merge): Repeatedly merge the sorted sublists back together to produce new sorted sublists until only one remains.

Builds on: Recursion — the same split-and-recurse pattern that painted Mondrians, now sorting lists.
You've got it when…
- Your
mergefunction combines two sorted lists into one sorted list. - Your
merge_sortfunction sorts any list by calling itself, then callingmerge. - You've tested both functions — including odd-length lists, an empty list, and a list of one item.
Collaboration & AI
Work: On your own. Compare test results with a neighbor, not code.
AI — AIAS Level 1, No AI: Implementing an algorithm from its description is the whole assignment. Follow the steps below yourself. What the levels mean.
The Algorithm
Part 1: The Merge Sort Process
- If the list has one item or fewer, it is already sorted — return the list.
- Find the middle of the list.
- Split the list into two halves: a left half and a right half.
- Sort the left half using merge sort.
- Sort the right half using merge sort.
- Merge the two sorted halves back together (see Part 2 below).
- Return the merged list.
Steps 4 and 5 are the recursion
"Sort the left half using merge sort" — the algorithm you are writing
calls itself, just like subdivide did. Step 1 is its stopping
condition, and it's why the whole thing ends: every split eventually
produces lists of one item, and those come back immediately.
Part 2: How to Merge Two Sorted Lists
You are given two already sorted lists (left and right).
- Create an empty list called
result. - Set two positions:
ifor the left list andjfor the right list, both starting at 0. - Repeat while both lists still have items left:
- Compare the current items,
left[i]andright[j]. - If
left[i]is smaller: add it toresultand moveiforward. - Otherwise: add
right[j]toresultand movejforward.
- Compare the current items,
- If there are remaining items in the left list, add all of them to
result. - If there are remaining items in the right list, add all of them to
result. - Return
result.
Why merging is the easy part
Both lists arrive sorted, so the smallest unclaimed item is always sitting at the front of one list or the other. Merging never searches — it just compares the two front items and takes the smaller one, like two checkout lines feeding one door.
Task
Implement the merge sort algorithm in Python.
How to tackle this? Tips:
-
Write the merge part first (Part 2 above).
Start with a function that accepts two lists,
leftandright:- This function should return a new list with the two lists merged in sorted order.
- Follow the steps in Part 2 of the algorithm above to write this function.
-
Test your
mergefunction with small lists.What does a good merge test look like?
Feed it two short sorted lists by hand — say
[2, 5, 9]and[1, 5, 6]— and check the output against what you'd expect. Then try uneven lengths and an empty list on one side. -
Now write a merge sort function (Part 1 above).
This function accepts a list of items to sort:
- This function will call your
mergefunction. - Follow the steps in Part 1 of the algorithm above to write this function.
- This function will call your
Warning
Merge sort functions return their results — merge_sort hands back a
new sorted list rather than rearranging the one you gave it. Forgetting
a return (in steps 1 or 7 of Part 1, or step 6 of Part 2) is the most
common way this lab produces a program that runs fine and sorts nothing.
Turn It In
- Your Python (.py) file with your
mergeandmerge_sortfunctions.
How It's Graded
This lab is worth up to 4 points. One score covers everything you turn in.
| Score | What it looks like |
|---|---|
| 4 — Excellent | merge combines two sorted lists correctly and merge_sort sorts by calling itself and then merge — both return their results — and your file shows the tests that prove it, including odd-length lists, an empty list, and a list of one. |
| 3 — Above Average | Both functions work; an edge case went untested, or a test that exists in your head but not in the file. |
| 2 — Average | merge works but merge_sort doesn't — usually a missing return, the bug that runs fine and sorts nothing. |
| 1 — Below Average | Neither function produces sorted output. |
| 0 — Failing | No file turned in. |