Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Lecture 6:
Efficiency Analysis
Tauseef Iftikhar
input: n
line no. cost iterations
1 c1 1
1: s0 2 c1 1
2: i 1 3 c2 n+1
3: while i n 4 c3 n.1
do 5 c3 n.1
4: s s +i
5: i i +1
6: end while
Example2: running time
input: n
line no. cost iterations
1 c1 1
1: s0 2 c1 1
2: i 1 3 c2 n+1
3: while i n 4 c3 n.1
do 5 c3 n.1
4: s s +i
T (n) = 2c1 + c2(n + 1) + 2c3n
5: i i +1
6: end while
Example2: running time
input: n
line no. cost iterations
1 c1 1
1: s0 2 c1 1
2: i 1 3 c2 n+1
3: while i n 4 c3 n.1
do 5 c3 n.1
4: s s +i
T (n) = 2c1 + c2(n + 1) + 2c3n
5: i i +1
T (n) = 2c1 + c2.n + c2 + 2c3.n
6: end while
Example2: running time
input: n
line no. cost iterations
1 c1 1
1: s0 2 c1 1
2: i 1 3 c2 n+1
3: while i n 4 c3 n.1
do 5 c3 n.1
4: s s +i
T (n) = 2c1 + c2(n + 1) + 2c3n
5: i i +1
T (n) = 2c1 + c2.n + c2 + 2c3.n
6: end while
T (n) = a.n + b
Example2: running time
input: n
line no. cost iterations
1 c1 1
1: s0 2 c1 1
2: i 1 3 c2 n+1
3: while i n 4 c3 n.1
do 5 c3 n.1
4: s s +i
T (n) = 2c1 + c2(n + 1) + 2c3n
5: i i +1
T (n) = 2c1 + c2.n + c2 + 2c3.n
6: end while
T (n) = a.n + b
Algorithm 1 search
1: found true
2: i 1
3: while (found = false) and (i n)
do
4: if x[i] = v then
5: found true
6: else
7: i i +1
8: end if
9: end while
Example 4: sequential search
Algorithm 2 search
line no cost
1: found true
1 1
2: i 1
2 1
3: while (found = false) and (i n)
3 f (n) + 1
do
4 f (n)
4: if x[i] = v then
5 g (n)
5: found true
7 h(n)
6: else
7: i i +1
T(n)=
8: end if
3 + 2f (n) + g (n) + h(n)
9: end while
Example 4: sequential search
Algorithm 3 search
line no cost
1: found true
1 1
2: i 1
2 1
3: while (found = false) and (i n)
3 f (n) + 1
do
4 f (n)
4: if x[i] = v then
5 g (n)
5: found true
7 h(n)
6: else
7: i i +1
T(n)=
8: end if
3 + 2f (n) + g (n) + h(n)
9: end while
Example 4: sequential search