Tutelage

Lessons › coding and computer science · taught by Leo

Bubble sort basics: how adjacent swaps work

Learn how bubble sort compares neighboring items, swaps out-of-order pairs, and builds a sorted list one pass at a time.

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

What bubble sort does

Bubble sort is a simple way to sort a list by comparing neighbors.

If two neighbors are out of order, you swap them.

Do that again and again until the biggest items drift to the end.

Big idea: one pass at a time

Bubble sort makes repeated passes through the list. Each pass pushes the largest unsorted value to the right.

Think of a bubble rising in water. The biggest value slowly moves rightward, one swap at a time.

After one full pass, the rightmost spot is settled.

Predict the next swap

In the pair 7 and 3, what should bubble sort do if it is sorting from small to large?

  1. Swap them
  2. Keep them
  3. Delete 3
Show answer

Swap them: Yes. Seven is bigger than three, so they are out of order and should swap.

One pass on a sample list

Watch one pass. I compare adjacent numbers from left to right.

Each swap moves a larger number one step to the right.

By the end of the pass, 5 has bubbled to the end.

Quick check: why swap?

In bubble sort, when do you swap two neighboring numbers?

  1. Only when the left number is bigger
  2. Only when they are equal
  3. Only at the very end
Show answer

Only when the left number is bigger: Correct. The swap happens only if the pair is in the wrong order.

Why one pass helps

A full pass guarantees the largest unsorted element reaches its final place at the end. That means the next pass can ignore that last spot.

This works because every comparison fixes one local mistake.

After enough passes, all the local fixes add up to a fully sorted list.

Sorted suffix grows

The right side becomes sorted first.

That sorted part gets bigger after each pass.

So the work gets smaller each round.

Common mistake to avoid

After one full pass, what is guaranteed about bubble sort?

  1. The largest unsorted item is at the end
  2. The whole list is fully sorted
  3. The smallest item is at the end
Show answer

The largest unsorted item is at the end: Exactly. One pass guarantees the largest unsorted item has reached the end.

Key idea: adjacent swaps only

Here is the heart of it: only neighbors are compared.

That small rule is repeated many times.

Simple rule, repeated often, gives the final sort.

Watch out: one pass is not done

A common mistake is thinking one pass means the whole job is done.

No. It usually takes many passes.

You keep going until a full pass makes no swaps.

Your turn: next pass result

After one pass, the list is [2, 3, 1, 4]. What is the key thing you know for sure?

  1. 4 is in its final spot at the end
  2. 2 is in its final spot at the start
  3. The list is fully sorted
Show answer

4 is in its final spot at the end: Yes. The biggest value, 4, is now at the end after one pass.

Bubble sort recap

Use this checklist: compare neighbors.

Swap if the pair is out of order.

After each pass, the right side is more sorted. Then repeat.

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