Evaluating
Running Time of Algorithm
DS / Running-Time 1
Principles of Running Time Analysis
• Measure time as a function of the input size
• Evaluate the worst-case performance for all inputs up to a given size
• Ignore constant factor
• Compare the functions that measure the time complexity of
algorithms by their growth rate:
– big-O for estimated upper bounds
– big- W for estimated lower bounds
DS / Running-Time 2
Running Time of Simple Statements
• Simple Statement
T(n) = O(1)
– arithmetic (+, *,…) operations
– logic operations (&&, ….)
– comparisons ( 0 and A[k]> key
A[k+1]:= A[k]
k := k-1
A[k+1] := key
T(n) = ?
DS / Running-Time 9
Running Time of if-statement
test
O (max (f (n), g (n)))
if-part else-part
O(f(n)) O(g(n))
DS / Running-Time 10
If Statement - example
if ( a[1][i] == 0)
for (i = 0, i 0 and A[k]> key
A[k+1]:= A[k] 4
k := k-1 5
A[k+1] := key 6
DS / Running-Time 15
Insertion sort - tree structure
Evaluate running time bottom-up
for
2 3 while 6
4
leaves = simple statements
internal nodes = compound statements
5
DS / Running-Time 16
Programs with Function Call
• Non Recursive function calls
– Evaluated like compound statements. Bottom up.
• Recursive function calls
– Running time represented by a recursive function
– Requires techniques for solving recursive functions.
DS / Running-Time 17