COE428-2
COE428-2
14
2 Running time
15
Timing analyses
Worst-case: (usually)
• T(n) = maximum time of algorithm
on any input of size n.
Average-case: (sometimes)
• T(n) = expected time of algorithm
over all inputs of size n.
• Need assumption of statistical
distribution of inputs.
Best-case: (bogus)
• Cheat with a slow algorithm that
works fast on some input.
16
• Analyzing algorithms
• Analyzing an algorithm has come to mean predicting the resources that the
algorithm requires.
• In general, the time taken by an algorithm grows with the size of the input
• Note that when a for or while loop exits in the usual way, due to the test
in the loop header, the test is executed one time more than the loop body.
19
• Running time
•The running time of the algorithm is the sum of running times for
each statement executed;
20
• Running time
21
• Running time
This running time can be expressed as an + b for constants a and b that depend on the
statement costs ci; it is thus a linear function of n.
T (n) = an + b
22
• Arithmetic series
• The summation
• Example: n = 4 4
k 1 2 3 4 10
k 1
• Which came up when we analyzed insertion sort is an arithmetic series and has the
value
• Example: n = 4 4
1
k
k 1 2
4(4 1) 10
23
• Running time
•Worst Case
• If the array is in reverse sorted order--that is, in decreasing
order--the worst case results.
24
• we find that in the worst case, the running time of INSERTION-SORT is
• This worst-case running time can be expressed as an2 + bn+ c for constants a, b, and c
that again depend on the statement costs ci; it is thus a quadratic function of n
25
•Running time
• We usually concentrate on finding the worst-case running
time: the longest running time for any input of size n.
•Reasons:
• The worst-case running time gives a guaranteed upper
bound on the running time for any input.
• For some algorithms, the worst case occurs often.
• For example, when searching, the worst case often occurs
when the item being searched for is not present, and
searches for absent items may be frequent.
26
• Order of growth
• Another abstraction to ease analysis and focus on the important features.
• Look only at the leading term of the formula for running time.
• Drop lower-order terms.
• Ignore the constant coefficient in the leading term.
• Example: For insertion sort, we already abstracted away the actual
statement costs to conclude that the worst-case running time is an2 + bn + c.
• Drop lower-order terms => an2 .
• Ignore constant coefficient => n2 .
• But we cannot say that the worst-case running time T (n) equals n2 .
• It grows like n2 . But it doesn’t equal n2 .
• We say that the running time is (n2)to capture the notion that the order
of growth is n2 .
•We usually consider one algorithm to be more efficient than another if its
worst case running time has a smaller order of growth.
27
Machine‐independent time
28
Asymptotic performance
p q q +1 r
5 2 4 6 1 3 2 6
5 2 4 6 1 3 2 6
5 2 4 6 1 3 2 6
Initial array 5 2 4 6 1 3 2 6
42
Merge sort
43