Course 3.1Lesson 4 of 5
To compare methods, count the work: the number of checks or comparisons they need, as a function of the length n of the list. Linear search needs n checks in the worst case, binary search needs about the number of times that n can be halved, and bubble sort needs n times n minus one over two comparisons, so doubling the list makes the sort four times slower. A method whose work grows slowly is better when the list is long, and the difference is huge for a million items. Counting is better than timing, because it does not depend on the computer. For a short list the difference does not matter, and a simple method is the right choice.
Try the code and the simulations beside the lesson.
After this lesson you can
You have two ways to search, and several ways to sort. Which is better? "Faster" is the answer, but how do we measure it? We could use a stopwatch. But the time depends on the computer, on what else it is doing, and on the program around it. A better way is to count the work: how many checks or comparisons the method makes, as the list gets longer.
Let n be the number of items. We ask: how does the number of steps depend on n?
Predict first, then run it. Look at the columns.
n checks in the worst case. 10 items, 10 checks. A million items, a million checks. The work grows in step with the list.n. 10 items, 4 checks. A million items, 20. The work grows very slowly. A list that is 1000 times longer needs only 10 more checks.n.What happens to the work when the list becomes twice as long?
| Method | Double the list, and the work |
|---|---|
| linear search | becomes twice as big |
| binary search | grows by one step |
| bubble sort | becomes four times as big |
The last row is the important one: if sorting 1000 items takes one second with bubble sort, sorting 2000 takes about four seconds, and 10 000 items takes about a hundred. This is why sorting a big list with a slow method is a disaster, while the same list is no problem for a fast method. Python's sorted needs only about comparisons: for a million items, about 20 million, and not 500 billion.
The numbers above come from a formula. Does a real program agree? Let us count the comparisons that bubble sort makes on random lists.
The bubble sort curve leaves the picture early. That steep climb is why nobody sorts a big list with it.
A rule of thumb for a programmer: make it correct first, then make it fast, and only if it is too slow. A clever method that is wrong is worse than a simple one that is right.
Timing depends on the machine and on luck. A count does not. If a friend says "my program took 3 seconds", you cannot compare it with yours. If a friend says "my method needs n squared steps", you know how it behaves on any computer, for any n.
Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
n.n. Binary search: about the number of halvings. Bubble sort: about n squared over two.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.