Sorting Algorithms

Intermediate · AdvancedAbout 15 minProgramming & algorithms

Computers sort things constantly — search results, playlists, prices from low to high. Here's how one of the simplest methods works, one step at a time.

Why Sorting Matters

A sorted list is easier for people to read — and far faster for computers to search. The Binary Search trick of "guess the middle, throw away half" only works when the list is already in order. Sorting once makes every search after that quick.

A step-by-step method for solving a problem is called an algorithm. There are many sorting algorithms. They all end up with the same sorted list — they just take different routes to get there, and some routes are much shorter than others.

Bubble Sort: Compare Neighbors

Picture a row of people you want to arrange from shortest to tallest. Bubble sort works like this:

  1. Look at the first two people. If the left one is taller, swap them.
  2. Move one place to the right and compare the next pair. Swap if needed.
  3. Keep going to the end of the row. The tallest person has now "bubbled up" all the way to the end — they're in their final place.
  4. Start again from the left, ignoring the end of the row that's already sorted.
  5. Stop when you get through a whole pass without making a single swap.

Each pass moves the biggest remaining item to its correct place — like a bubble rising to the top.

Try it yourself

0Comparisons
0Swaps
0Passes

How Much Work Is That?

In the worst case — a list that starts completely backwards — bubble sort has to compare nearly every item with nearly every other item. The work grows much faster than the list does:

Items to sortComparisons (worst case)
1045
1004,950
1,000499,500
1 millionabout 500 billion

Ten times more items means roughly a hundred times more work. For a class list, that's no problem. For a million online orders, it's far too slow.

Faster Ways to Sort

Because sorting matters so much, computer scientists have invented quicker methods. One of the best known is merge sort, which uses a "divide and conquer" idea:

  • Split the list in half, and keep splitting until every piece is just one item.
  • Merge the pieces back together two at a time, always keeping them in order — like two people each holding a sorted hand of cards, combining them by repeatedly taking the smaller front card.

Sorting a million items takes merge sort around 20 million steps instead of 500 billion. Same sorted result, a tiny fraction of the work.

Different algorithms can solve the same problem correctly — but how much work they take can be wildly different. Choosing a good algorithm matters.

Fast Enough — and Far Too Slow

Bubble sort is slow on huge lists, but it still finishes in a sensible amount of time. Computer scientists call that reasonable time: as the list grows, the work grows by multiplying (10× more items, about 100× more work).

Some problems are much worse. To try every possible order of a delivery driver's stops, adding justone more stop multiplies the work again. With 10 stops there are over 3 million orders; with 20 there are more than a billion billion. That's unreasonable time — no computer could ever finish checking them all.

For problems like that, programmers use a heuristic: a sensible shortcut that finds a good answer quickly, even if it isn't guaranteed to be the very best. For the driver, "always go to the nearest stop next" is a heuristic.