2IL50 Data Structures: 2018-19 Q3 Lecture 2: Analysis of Algorithms
2IL50 Data Structures: 2018-19 Q3 Lecture 2: Analysis of Algorithms
2018-19 Q3
InsertionSort(A)
1. initialize: sort A[1]
2. for j = 2 to A.length
3. do key = A[j]
4. i = j-1
5. while i > 0 and A[i] > key
6. do A[i+1] = A[i]
7. i = i-1
8. A[i+1] = key
Analysis of algorithms
Analyze the running time as a function of n (# of input elements)
best case
average case
worst case
An algorithm has worst case running time T(n) if for any input of
size n the maximal number of elementary operations executed is
T(n).
elementary operations
add, subtract, multiply, divide, load, store, copy, conditional and
unconditional branch, return …
Linear Search
Input: increasing sequence of n numbers A = ‹a1, a2, …, an› and value v
Output: an index i such that A[i] = v or NIL if v not in A
LinearSearch(A, v) v=7
1. for i = 1 to n
2. do if A[i] = v
3. then return i 1 3 4 7 8 14 17 21 28 35
4. return NIL
Running time
best case: 1
average case: n/2 (if successful)
worst case: n
Binary Search
Input: increasing sequence of n numbers A = ‹a1, a2, …, an› and value v
Output: an index i such that A[i] = v or NIL if v not in A
BinarySearch(A, v)
1. x = 1
v=7
2. y = n + 1
3. while x + 1 < y
4. do h = ⌊(x + y)/2⌋ 1 3 4 7 8 14 17 21 28 35
5. do if A[h] ≤ v then x = h
6. else y = h
7. if A[x] = v then return x else return NIL
Running time
best case: 1
average case: log n
worst case: log n
Analysis of algorithms: example
n=10 n=100 n=1000
InsertionSort: 15 n2 + 7n – 2 1568 150698 1.5 x 107
10466 204316 3.0 x 106
MergeSort: 300 n log n + 50 n
InsertionSort InsertionSort
6 x faster 1.35 x faster
Θ(g(n)) = { f(n) : there exist positive constants c1, c2, and n0 such
that c1g(n) ≤ f(n) ≤ c2g(n) for all n ≥ n0 }
Θ(g(n)) = { f(n) : there exist positive constants c1, c2, and n0 such that
c1g(n) ≤ f(n) ≤ c2g(n) for all n ≥ n0 }
c2g(n)
f(n)
c1g(n)
0
n0 n
Θ-notation
Let g(n) : N → N be a function. Then we have
Θ(g(n)) = { f(n) : there exist positive constants c1, c2, and n0 such
that c1g(n) ≤ f(n) ≤ c2g(n) for all n ≥ n0 }
Θ(g(n)) = { f(n) : there exist positive constants c1, c2, and n0 such
that c1g(n) ≤ f(n) ≤ c2g(n) for all n ≥ n0 }
cg(n)
f(n)
0
n0 n
Ω-notation
Let g(n) : N → N be a function. Then we have
f(n)
cg(n)
0
n0 n
Asymptotic notation
n
f(n) = O(i) means there is one function g(i) such that
i 1 n
f(n) = g(i) and g(i) = O(i)
i 1
2n2 + O(n) = Θ(n2) means for each function g(n) with g(n)=O(n)
we have 2n2 + g(n) = Θ(n2)
Quiz
1. O(1) + O(1) = O(1) true
Upper bound: Let T(n) be the worst case running time of InsertionSort on an
array of length n. We have
n n
T(n) = O(1) + { O(1) + (j-1)∙O(1) + O(1) } = O(j) = O(n2)
j 2 j 2
T(⌊n/2⌋) + T(⌈n/2⌉)
O(1) if n = 1
T(n) =
T(⌊n/2⌋) + T(⌈n/2⌉) + Θ(n) if n > 1
Alternatively: Guess the solution and use the substitution method to prove
that your guess is correct.
How to guess:
1. expand the recursion
2. draw a recursion tree
The master theorem
Let a and b be constants, let f(n) be a function, and let T(n)
be defined on the nonnegative integers by the recurrence
T(n) = aT(n/b) + f(n) can be rounded up or down
Then we have:
note: (logba) - ε
1. If f(n) = O(nlog a – ε) for some constant ε > 0, then T(n) = Θ(nlog a). b
b
3. If f(n) = Ω(nlog a + ε) for some constant ε > 0, and if af(n/b) ≤ cf(n) for
b
some constant c < 1 and all sufficiently large n,
then T(n) = Θ(f(n))
The master theorem: Example
T(n) = 4T(n/2) + n3
Case 3 of the master theorem gives T(n) = Θ(n3), if the regularity condition
holds.
choose c = ½ and n0 = 1
➨ T(n) = Θ(n3)
The substitution method
The Master theorem does not always apply
n/2 n/2
n n
n2
(n/2)2 (n/2)2
n2 n2
n n
n = n0 = 2 is a base case
⌊3/2⌋ = 1, ⌊4/2⌋ = 2 ➨ 3 must also be
Need more base cases?
base case
Base cases:
n = 2: T(2) = 2T(1) + 2 = 2∙2 + 2 = 6 = c 2 log 2 for c = 3
n = 3: T(3) = 2T(1) + 3 = 2∙2 + 3 = 7 ≤ c 3 log 3
The substitution method
2 if n = 1
T(n) =
2T(⌊n/2⌋) + n if n > 1
i
i1
½ n(n+1) = Θ(n2)
n
i
i1
2
⅙ n(n+1)(2n+1) = Θ(n3)