edubun 한국어

Sorting 1: bubble, selection and insertion sort

Selection sort makes exactly 45 comparisons on any list of 10 values, whether the list starts shuffled or already in order.

Putting things in order

Sorting means rearranging a list so its values go from smallest to largest (or by name, date, price, any order you can compare). It is one of the most common jobs a computer does: the contacts on a phone, search results ranked by relevance, a spreadsheet column sorted by date. Sorting also makes other jobs easier. As the last unit showed, a sorted list can be searched with binary search, and duplicates in a sorted list sit next to each other.

This unit covers three simple methods. All three work by comparing two values at a time and moving values around inside the same list. They are short to write and easy to follow, and all three slow down sharply on long lists. Watching why they slow down is the best preparation for the faster methods in the next unit. Throughout, we count two things: comparisons (asking "is this value bigger than that one?") and swaps (exchanging two values).

Bubble sort: swap neighbours that are out of order

Bubble sort walks along the list comparing each pair of neighbours. When the left one is bigger, it swaps them. After one full pass, the largest value has been carried step by step to the far right, like a bubble rising, and it never needs to move again. The next pass can stop one place earlier. A common improvement is to watch for a pass with no swaps at all: that means everything is already in order, so the sort can stop early.

Follow it on the list [5, 1, 4, 2, 8]. The first pass compares 5 and 1 (swap), 5 and 4 (swap), 5 and 2 (swap), 5 and 8 (leave): the list is now [1, 4, 2, 5, 8] and 8 is in place. The second pass compares 1 and 4, 4 and 2 (swap), 4 and 5, giving [1, 2, 4, 5, 8]. The third pass compares 1 and 2, then 2 and 4, makes no swap and stops. Total: 4 + 3 + 2 = 9 comparisons and 4 swaps.

function bubbleSort(a) {
  for (let pass = 0; pass < a.length - 1; pass++) {
    let swapped = false;
    for (let j = 0; j < a.length - 1 - pass; j++) {
      if (a[j] > a[j + 1]) {
        [a[j], a[j + 1]] = [a[j + 1], a[j]];  // swap
        swapped = true;
      }
    }
    if (!swapped) break;  // already sorted
  }
  return a;
}

Selection sort: find the smallest, put it next

Selection sort works the way many people sort a hand of cards. Scan the whole list for the smallest value and swap it into the first position. Then scan the rest for the smallest remaining value and swap it into the second position, and so on. On [5, 1, 4, 2, 8] the first scan finds 1 and swaps it with 5; the second finds 2 and swaps it with 5; the last two scans find 4 and 5 already in place. That is only 2 swaps, but the scans still compare 4 + 3 + 2 + 1 = 10 times.

Selection sort never stops early. Even on a list that is already sorted, it still has to check every remaining value to be sure it has the smallest. For n values the comparisons always add up to (n − 1) + (n − 2) + … + 1 = n(n − 1)/2. For 10 values that is 45; for 100 values, 4,950; for 1,000 values, 499,500. Its strength is the small number of swaps: at most n − 1, which matters when moving a value is expensive.

Insertion sort: slide each new value into place

Insertion sort keeps the left part of the list sorted and grows it one value at a time. It takes the next value and moves it left, swapping with its neighbour, until the neighbour is not bigger. Then that value is in place among the sorted ones. On a list that is already nearly in order, each new value moves only a step or two, so insertion sort is very quick there. On [5, 1, 4, 2, 8] it makes 7 comparisons and 4 swaps.

Loading

Drag to turn. Scroll or pinch to zoom.

The list [5, 1, 4, 2, 8]. Switch between the three methods and check the counts in this unit. Then choose Reversed or Already sorted to see best and worst cases.

Counting the swaps: inversions

There is a neat way to predict how many swaps bubble sort and insertion sort need. Count the inversions: pairs of values that are in the wrong order relative to each other. In [5, 1, 4, 2, 8] the inversions are (5, 1), (5, 4), (5, 2) and (4, 2), four in all. Each swap of two neighbours that are out of order fixes exactly one inversion and creates none, so both methods make exactly 4 swaps. A reversed list of n values has every pair inverted, n(n − 1)/2 inversions, which is the worst case.

MethodExampleSorted (5 values)Reversed (4 values)
Bubble sort9 / 44 / 06 / 6
Selection sort10 / 210 / 06 / 2
Insertion sort7 / 44 / 06 / 6

Each cell gives comparisons / swaps. The example list is [5, 1, 4, 2, 8]. Bubble sort here is the version that stops after a pass with no swap.

All three methods share the same weakness. In the worst case each one compares roughly every pair of values, about n²/2 comparisons. Double the list and the work goes up four times. Ten times the list means a hundred times the work. For a few dozen values that hardly matters; for a million it is hopeless, and the next unit shows how to do far better.

comparisonAsking whether one value is bigger than another
swapExchanging the positions of two values
passOne trip along the list
bubble sortSwap neighbours that are out of order; the largest rises to the end each pass
selection sortFind the smallest remaining value and swap it into the next place
insertion sortSlide each new value left into the sorted part
inversionA pair of values in the wrong order relative to each other
n(n − 1)/2The number of pairs among n values
  1. How many comparisons does selection sort make on any list of 10 values?
  2. How many inversions does [3, 1, 2] have?
  3. How many swaps does insertion sort make on [4, 3, 2, 1]?
  4. After the first pass of bubble sort, which value is certainly in its final place?
    • The smallest, at the front
    • The largest, at the end
    • The middle value
    • None yet
  5. Which method does the fewest swaps on [5, 1, 4, 2, 8]?
    • Bubble sort
    • Selection sort
    • Insertion sort
  6. A list is already sorted. Which method still makes n(n − 1)/2 comparisons?
    • Bubble sort with the early stop
    • Selection sort
    • Insertion sort
  7. How many comparisons does bubble sort (with the early stop) make on [5, 1, 4, 2, 8]?
  8. A list of 1,000 values becomes 2,000 values. Roughly how does the worst-case work of insertion sort change?
    • It doubles
    • It goes up about four times
    • It goes up by 1,000 steps
    • It stays the same
  9. Which method is the best choice for a list that is nearly sorted already?
    • Insertion sort
    • Selection sort
    • Any of them, they are all equal
  10. How many pairs can be made from 100 values (the worst-case comparison count, n(n − 1)/2)?
  11. Put the list [3, 1, 2] in the order it passes through during insertion sort.
Key points
  • Bubble sort swaps neighbours; each pass carries the largest value to the end.
  • Selection sort always makes n(n − 1)/2 comparisons and at most n − 1 swaps.
  • Insertion sort is fast on nearly sorted lists.
  • Bubble and insertion sort make one swap per inversion.
  • All three take about n² steps in the worst case.