Sorting and searching

Step 1 of 6

Bubble sort

Compare neighbours, swap if needed

Why this matters. Computers sort constantly: search results, a leaderboard, your inbox by date. To sort, a computer can only do one small thing at a time: look at two values and decide which comes first. Every sorting method is a plan for which two to look at next.

Bubble sort is the simplest plan. Walk along the row comparing each pair of neighbours. If the left one is bigger, swap them. One walk along the row is a pass, and after each pass the biggest card left has bubbled all the way to the end, where it stays.

Then go again on the cards that are left. When a whole pass goes by without a single swap, everything is in order.

Your task: Sort the six cards with bubble sort: for each highlighted pair, swap or leave them, and at the end of each pass decide whether to go again.

Comparisons 0Swaps 0
514283

Pass 1: compare 5 and 1.

Sign in to save your progress.