Lec - 2 Running Time
Lec - 2 Running Time
Decision making
PRIMITIVE OPERATIONS
Ex: Find min value of an array?
Min(A, 1 , n)
m=A(1)
for i=2 to n
if A(i) <m
m=A(i)
Array size 106
Ex: sorting numbers:
2𝑛 (exponential)
Input size 𝑛 𝑛2 log10 𝑛
10 10 100 1
logarithmic
constant
𝑛3 > 𝑛2
𝑛2 > log 𝑛
𝑛2 log 𝑛 > n log 𝑛2
𝑛 log 𝑛 < 𝑛2
For i=1 to n
𝑛 print ‘Hi’
For i=1 to n
if i <11
10 print ‘Hi’
else
break
Nested loop:
For i=1 to n
𝑛
Dependent nested loop:
𝑛 𝑛2 for j=1 to n
𝑖=1 𝑛 For i=1 to n
print ‘Hi’
𝑛 for j=1 to I 𝑖 = 1, 1
𝑖 = 2, 2
𝑖 print ‘Hi’ .
𝑖=1
.
.
𝑖 = 𝑛, 𝑛
Why study summations?
1. In general, the running time of a loop can be expressed as the sum
of the running time of each iteration.
2. Summations come up in solving recurrences.
𝑛 𝑛(𝑛+1)
𝑖=1 𝑖 =
2
𝑛
𝑖=1 𝑐𝑜𝑛𝑠𝑡𝑎𝑛𝑡 𝑜𝑟 𝑛 = ∗ = 𝑛2
log2 𝑛 𝑖
𝑖=1 2 = 2𝑛 − 1
log2 𝑛
𝑖=1 𝑐𝑜𝑛𝑠𝑡𝑎𝑛𝑡 = ∗
𝑛
𝑖=𝑗 1 =𝑛−𝑗+1
𝑛 𝑥 𝑛+1 −1
𝑖=1 𝑥 𝑖
= x constant
𝑥−1
𝑛 2 2𝑛3 +3𝑛2 +𝑛
𝑖=1 𝑖 =
6
𝑛 1
𝑖=1 𝑖 = log 𝑛 + 1
For (i=1, i≤ 𝑛,i++)
𝑛
1 Print i
𝑖=1
end
𝑇 𝑁 = 1= 1 ∗ 𝑛 =𝑛
𝑖=1
𝑛−1 For (i=n-1, i≥ 1,i--)
1 Print i
𝑖=1
end
𝑛−1
𝑇 𝑁 = 1= 1 ∗ 𝑛−1 =𝑛−1
𝑖=1
log2 𝑛 For (i=1, i≤ 𝑛,i*=2)
1 Print i
𝑖=1
end
log2 𝑛
𝑇 𝑁 = 1= 1 ∗ log 2 𝑛 = log 2 𝑛
𝑖=1
log2 𝑛 For (i=1, i≤ 𝑛,i/=2)
1 Print i
𝑖=1
end
log2 𝑛
𝑇 𝑁 = 1= 1 ∗ log 2 𝑛 = log 2 𝑛
𝑖=1
𝑛
For (i=1, i≤ 𝑛,i++)
𝑡 For (j=1, j≤ 𝑛,j++) 𝑛
𝑖=1 1
print “hi”
𝑖=1
end
end 𝑛 𝑛 𝑛
𝑇 𝑁 = 1= 𝑛 = 𝑛2
𝑖=1 𝑗=1 𝑖=1
Our first algorithm, insertion sort, solves the sorting problem.
𝑛
𝑇 𝑁 = 𝑡
𝑗=2
If the array is already sorted, Thus 𝑡𝑗 If the array is in reverse sorted order
= 1 for j = 2;3; … ;n, and the best-case that is, in decreasing order—the
running time is worst case results. We must compare
each element A[j] with each element
𝑛 in the entire sorted subarray A[1 .. j -
𝑇 𝑁 = 1=𝑛 1], and so 𝑡𝑗 = j for j = 2;3; … ;n
𝑗=2
𝑛
𝑛(𝑛 + 1) 𝑛2 + 𝑛
𝑇 𝑁 = 𝑗= =
2 2
𝑗=2
Ο(𝑛)
Ο(𝑛2 )