What happens to the work when the list doubles: bubble sort grows like the square of the length, and merge sort only a little faster than the length itself. The graph that compares them, big-O notation, and why a good algorithm beats a fast computer.
Step 1 of 6 · Doubling the list
Step 1 of 6
Doubling the list
How long a sort takes depends on the computer, but the number of comparisons does not. So we ask a question that does not depend on the computer: when the list doubles, by how much does the work grow? The answer is the algorithm’s growth rate.
Step 2 of 6
Doubling with merge sort
Bubble sort compares pairs, and the number of pairs grows fast. Merge sort works differently: levels, and at each level work in proportion to . Together, about .
Step 3 of 6
The growth board
Show how it grows is on Growth: a glass board rises over the stand with the number of comparisons against List size. The orange ribbon is , every pair; the blue ribbon is ; the white curve is the algorithm you chose, averaged over several starting rows; and the dot is the sort running now.
Step 4 of 6
Big-O
To talk about growth rates without exact numbers, we write big-O: keep only the fastest-growing term, and drop the constant factors. Linear search is , binary search , and merge sort .
Change Algorithm and Starting order and compare the white curve with the two ribbons.
Step 5 of 6
Finding the size
When the formula is known, you can also go backwards: from the amount of work to the size of the list.
Step 6 of 6
A better algorithm
The difference between and looks small at sixty-four blocks. At a million it is enormous: is about , and about .
Loading the interactive version…
Loading the interactive version…
Each step sets up the Scene for you, and a control marked “fixed” stays put for that step.
Practice · Predict, then look
Bubble sort on a Reversed row of 16 blocks makes 120 comparisons. List size will double to 32. How many comparisons will there be?
Practice · Predict, then look
Bubble sort on a Reversed row of 16 blocks makes 120 comparisons. List size will double to 32. How many comparisons will there be?
Practice · Predict, then look
Now Merge: on a Reversed row of 16 blocks it makes 32 comparisons. What will happen when List size doubles to 32?
Practice · Predict, then look
Now Merge: on a Reversed row of 16 blocks it makes 32 comparisons. What will happen when List size doubles to 32?
Practice · Calculate
Show how it grows is on Growth: the orange ribbon is , the number of pairs. Bubble sort on a Reversed row of 64 blocks compares every pair once. How many comparisons is that?
Practice · Calculate
Show how it grows is on Growth: the orange ribbon is , the number of pairs. Bubble sort on a Reversed row of 64 blocks compares every pair once. How many comparisons is that?
Practice · Choose
On a reversed row bubble sort makes comparisons. How is that written in big-O notation?
Practice · Choose
On a reversed row bubble sort makes comparisons. How is that written in big-O notation?
Practice · Reach the target
Bubble, Reversed. Change List size until the sort makes exactly 190 comparisons, and press Check.
Practice · Reach the target
Bubble, Reversed. Change List size until the sort makes exactly 190 comparisons, and press Check.
Practice · Choose
A million shuffled items have to be sorted. Computer A does comparisons a second and runs bubble sort, about comparisons. Computer B is a hundred times slower, comparisons a second, and runs merge sort, about comparisons. Which finishes first?
Practice · Choose
A million shuffled items have to be sorted. Computer A does comparisons a second and runs bubble sort, about comparisons. Computer B is a hundred times slower, comparisons a second, and runs merge sort, about comparisons. Which finishes first?
Lesson complete. Every practice question here is done.
