On this page, you will learn that some correct algorithms take too long to be practical.
time function
with each of these input functions:
numbers from () to ()
?You can classify algorithms by the amount of time they take to run.
To classify an algorithm, look at the number of steps it takes to complete the algorithm, compared to the size of the input.
Polynomial time includes constant (n0), sublinear, linear (n1), quadratic (n2), cubic (n3), etc. functions:
algorithm efficiency | If you double the size of input, the time... |
example algorithm |
---|---|---|
constant | stays the same (multiplies by 20 which is 1) | item ( 87) of () |
sublinear | multiplies by some number between 1 and 2 (between 20 and 21) | binary search: position of () in sorted list |
linear | multiplies by 2 (which is 21) | linear search: position of () in unsorted list |
quadratic | multiplies by 4 (which is 22) | some kinds of sorting algorithms |
cubic | multiplies by 8 (which is 23) | some gene mapping algorithms used in biology |
It's rare to find polynomial time algorithms that take more than cubic (n3) time.
What if algorithm takes an amount of time that's between two categories?
One kind of problem whose solution often ends up unreasonable is an optimization problem (such as "find the best" or "find the smallest").
One reason it's worth learning these categories is that in writing programs, you often need to solve a problem for which there are already established solutions. For example, you've learned that searching for something in an unordered list takes linear time, but if the list is sorted, you can search it faster (in sublinear time). So when you're writing a program that needs to search through a list repeatedly, you'll know that it's worthwhile to sort the list before the searching. Knowing about standard algorithms that already exist can help you construct new algorithms.
time function
with large inputs to Alphie's way
(say, multiples of 100). Then use the techniques from Lab 2 to plot the graph.
Task | Small Town (population 1,000) |
Mid-sized Town (population 10,000) |
Large Town (population 100,000) |
---|---|---|---|
Entering Data | 2 hours | 20 hours | 200 hours |
Backing up Data | 0.5 hours | 5 hours | 50 hours |
Searching through Data | 5 hours | 15 hours | 25 hours |
Sorting Data | 0.01 hour | 1 hour | 100 hours |