Algorithm 2 Getting Started
Algorithm 2 Getting Started
T(n)=an+b
T(n)=an2+bn+c
Running Time
• The worst-case running time of an algorithm gives us an upper
bound on the running time for any input. Knowing it provides a
guarantee that the algorithm will never take any longer. We
need not make some educated guess about the running time
and hope that it never gets much worse
Running Time
• For some algorithms, the worst case occurs fairly often. For
example, in searching a database for a particular piece of
information, the searching algorithm’s worst case will often
occur when the information is not present in the database. In
some applications, searches for absent information may be
frequent.
Running Time
• The “average case” is often roughly as bad as the worst case.
Suppose that we randomly choose n numbers and apply
insertion sort. How long does it take to determine where in
subarray A[1…,j-1] to insert element A[j] ?