SLIDE 4 http://www.cp.eng.chula.ac.th/faculty/spj
For Loop
∑∑
= =
Θ
m i i j 1 1
) 1 (
∑
=
Θ =
m i
i
1
) ( ) (
2
m Θ = + Θ = 2 ) 1 (m m Θ =
∑
= m i
i
1
for ( i = 1 to m ) for ( j = 1 to i ) s += A[i][j]
http://www.cp.eng.chula.ac.th/faculty/spj
For Loop
for ( i = 2 to m-1 ) for ( j = 3 to i ) s += A[i][j]
∑∑
− = =
Θ
1 2 3
) 1 (
m i i j
∑
− =
Θ =
1 2
) (
m i
i ) (
2
m Θ = Θ + Θ = ) ( 2
2
m m Θ =
∑
− = 1 2 m i
i
http://www.cp.eng.chula.ac.th/faculty/spj
While Loop
while ( n > 0 ) { … n = n - 1 } while ( n > 0 ) { … n = n / 2 } i = 0; j = n while ( i < j ) { … i++; j--; }
Θ(n) Θ(log n) Θ(n)
http://www.cp.eng.chula.ac.th/faculty/spj
Θ( i )
While Loop: Insertion Sort
Insertion_Sort( A[1..n] ) for j = 2 to n { key = A[j] i = j-1 while i>0 and A[i]>key { A[i+1] = A[i] i = i-1 } A[i+1] = key }
Ο( i )