Tutelage

Lessons › coding and computer science · taught by Leo

Merge sort explained: splitting and merging

Learn how merge sort splits lists into small pieces, then merges sorted halves by always choosing the smaller front item.

Learn this with Leo, freeA voiced lesson with a live whiteboard, quizzes and a tutor you can call, at tutelage.app

What merge sort does

Merge sort is a way to sort numbers by splitting them into smaller groups first.

Then it merges those small sorted groups back together in order.

The big idea is: break the problem down, then build the answer back up.

Split until the pieces are tiny

Each split makes the lists easier to sort because tiny lists are simple to compare.

If a list has one item, it is already sorted.

So merge sort keeps splitting until every piece has size one.

That is why the first half of the algorithm is all about making small, easy jobs.

Predict the first split

If you start with [8, 3, 5, 1], what should merge sort do first?

  1. Split it into [8, 3] and [5, 1]
  2. Sort it immediately from left to right
  3. Swap 8 and 1 first
Show answer

Split it into [8, 3] and [5, 1]: Yes. The first move is to split the list into two halves.

Work one merge by hand

Let’s merge two sorted lists: [2, 7] and [5, 9].

Compare the front items. Take the smaller one each time.

Keep going until one list runs out, then copy the leftovers.

Quick check on merging

You are merging [4, 8] and [5, 6]. What should you take first?

  1. 4
  2. 5
  3. 8
Show answer

4: Correct. You always take the smaller front item first.

Why the merge step works

The merge works because both halves are already sorted, so the smallest remaining item must be at one of the two fronts.

This is the key reason merge sort is fast to reason about.

You never search the whole list again. You only compare the two front items.

That keeps each merge simple and organized.

One full merge sort run

A full merge sort goes in two phases.

First: split all the way down.

Second: merge back up until one sorted list remains.

So the shape is like a tree going down, then back up.

Watch out: don't unsort it

A common mistake is to mix up the order while merging.

Do not jump around the lists. Only look at the front of each sorted list.

That one rule keeps the merge correct.

Your turn: merge two lists

Merge [1, 4] and [2, 3]. What is the next item added first?

  1. 1
  2. 2
  3. 4
Show answer

1: Yes. The smaller front item is 1, so it goes first.

Merge sort checklist

Here is the whole method in four steps.

Split until the pieces are size one.

Merge sorted pieces by taking the smaller front item.

When one side is empty, copy the leftovers.

Big picture of merge sort

Merge sort is powerful because splitting makes the work simple, and merging keeps each step organized.

You can think of it as divide, then conquer.

It is not about clever jumping. It is about repeated simple choices.

That is the whole algorithm.

Learn this with Leo, freeA voiced lesson with a live whiteboard, quizzes and a tutor you can call, at tutelage.app