Lesson 1p
Lesson 1p
l How do we compare the efficiency of different l The best way is to compare algorithms by the
algorithms? amount of work done in a critical loop, as a
l Comparing execution time: Too many function of the number of input elements (N)
assumptions, varies greatly between different l Big-O: A notation expressing execution time
computers (complexity) as the term in a function that
l Compare number of instructions: Varies greatly increases most rapidly relative to N
due to different languages, compilers, l Consider the order of magnitude of the
programming styles... algorithm
l Best case: O(1) (we find the name immediately) l For target names in the very beginning of the
l Worst case: O(log N) (we find the name after phone book, algorithm 1 can be faster
2
cutting the space in half several times) l Algorithm 2 will be faster in every other case
l Average case: O(log N) (it takes a few steps to l Success of algorithm 2 relies the fact that the
2
find the name) phone book is sorted
- Data structures matter!
Figure 12.1 Example of straight selection sort (sorted elements are shaded)