Bubble Sort

Imagine you have a Row of unsorted colored blocks, and you want to line them up from shortest to tallest.

Bubble sort is a game where you only look at two blocks next to each other at a time.

Here is how you play:

The Line-Up Game

  1. Start at the beginning of the line and look at the first two blocks.
  2. Compare them: Is the left one taller than the right one?
    • If yes, they swap places!
    • If no, they stay right where they are.
  3. Move over by one block and look at the next pair. Do the exact same thing.
  4. Keep going all the way to the end of the line.

By the time you reach the end of the line, the very tallest block will have “bubbled” its way to the very last spot, just like a bubble rising to the top of a glass of soda!

Do It Again!

Because only the tallest block is perfectly in place, you have to go back to the front of the line and play the game all over again.

Each time you walk down the line, the next tallest block bubbles up to its correct spot. You keep doing this until you can walk from the start to the end without having to swap any blocks at all.

Once you can walk through the whole line without a single swap, the game is over, and your blocks are perfectly sorted!

Here are some additional Resources to help you understand Bubble Sort further.

Interactive Visualizations

  • Visualgo.net – Bubble Sort: This is one of the best tools for watching algorithms work. You can create your own list of numbers, slow down the animation, and watch the numbers literally swap places step-by-step.
  • Sorting.at: A beautifully clean visualizer. You can select “Bubble Sort” from the menu, click start, and watch the bars rearrange themselves with a clear highlight on which two elements are being compared.
  • Algorithm Visualizer: For when you get a little older and want to see the actual code side-by-side with the moving boxes. It shows exactly which line of code is running as the blocks move.

Fun Ways to Learn More

  • Bubble Sort Dance (YouTube): A famous and hilarious video by Sapientia University where folk dancers demonstrate exactly how bubble sort works by swapping places if the person on the left is taller than the person on the right.
  • GeeksforGeeks K-12: Bubble Sort: If you want a simple written breakdown with colorful diagrams that show each “pass” or trip down the line of blocks.
Andrew Thawley Avatar