# 03 - Analysis of Iterative Nonrecursive Algorithm - Exercise

```Analysis of Iterative
(Nonrecursive) Algorithm
Hopefully, you’ll be able to:
• Understand how to calculate for the analysis of iterative algorithm
• When you see a code, you can estimate its running time
Example
A( )
A( )
{
{
for (๐ = 1 ๐ก๐ ๐){
int ๐, ๐;
for (๐ = 1 ๐ก๐ ๐){
printf (“Salam”);
for (๐ = 1 ๐ก๐ ๐){
}}
printf (“Salam”);
}}}
&copy; S. Turaev CSC 3102 DSA II
3
Analysis of Nonrecursive Algos
Example: Find the value of the largest element in a list of
n real numbers.
Algorithm MaxElement(A 0. . n − 1 )
// Input: An array A 0. . n − 1
//Output: The value the max element in A
max ← A[0]
for i ← 1 to n − 1 do
Two operations in the loop
if A i &gt; max
Basic operation: the
max ← A[i]
comparison (it is executed in
return max
each repetition of the loop!)
&copy; S. Turaev CSC 3102 DSA II
4
Analysis of Nonrecursive Algos
Analysis:
โช n is the number of times the comparison is
executed.
โช The number of comparisons will be the same.
โช No need to distinguish among the worst, average
and best cases.
n−1
C n = เท1=n−1∈ Θ n .
i=1
&copy; S. Turaev CSC 3102 DSA II
5
General Plan for Analysis
โช Decide on parameter(s) indicating input size(s)
โช Identify algorithm’s basic operation(s)
โช Determine worst, average, and best case efficiencies
โช Set up a sum for the number of times the basic
operation(s) is (are) executed
โช Simplify the sum using standard formulas and rules
&copy; S. Turaev CSC 3102 DSA II
6
Analysis of Nonrecursive Algos
Example: The element uniqueness problem: check if all
elements in a given array of n elements are distinct.
Algorithm UniqueElement(A 0. . n − 1 )
// Input: An array A 0. . n − 1
//Output: Return “True” if all elements in A are distinct
&amp; “False” otherwise
for i ← 0 to n − 2 do
for j ← i + 1 to n − 1 do
if A i = A[j] return False
return True
&copy; S. Turaev CSC 3102 DSA II
7
Analysis of Nonrecursive Algos
โช The input size: n, the number of the elements in an array
โช The basic operation: the comparison operation in the
innermost loop
โช The number of comparisons depends not only on n but
also on if there are equal elements in the array, if there
are, which array positions they occupy.
โช The worst-case inputs:
• arrays with no equal elements
• arrays with the last two equal elements only
&copy; S. Turaev CSC 3102 DSA II
8
Analysis of Nonrecursive Algos
โช Worst-case analysis:
n−2 n−1
Cworst n = เท เท 1
n−2
i=0 j=i+1
= เท n−1 −
i+1 −1
i=0
n−2
= เท n − 1 −i = n− 1 + n −2 + โฏ+ 1
i=0
n n−1
1 2
=
≈ n ∈ Θ n2
2
2
&copy; S. Turaev CSC 3102 DSA II
9
Exercise 1
A( )
A( )
{
{
for (๐ = 1 ๐ก๐ ๐){
int ๐, ๐;
for (๐ = 1 ๐ก๐ ๐){
printf (“Salam”);
for (๐ = 1 ๐ก๐ ๐){
}}
printf (“Salam”);
}}}
&copy; S. Turaev CSC 3102 DSA II
10
Exercise 2
a) 1+2+3+4+….+100 = ? (Generalize: if 1+2+3+4+….+n ?)
b) 10 + 20 + 30 + 40 + … + 1000 = ? (Generalize)
c) ๐ − 1 + ๐ − 2 + ๐ − 3 + โฏ + 1 =?
d) 1 + 3 + 5+ 7 +9 + … + 999 = ? (Generalize)
e) 1 + 4 + 9 + 16 + 25 =?
f) 1 + 4 + 9 + 16 + โฏ + 100 =?
Exercise 3
a)
๐+1
σ๐=3 1
b)
๐+1
σ๐=3 ๐
c)
๐
๐
σ๐=1 σ๐=1 ๐๐
Exercise 4 &amp; 5 A(n)
{
A(n)
int ๐, ๐, ๐, ๐;
{
for (๐ = 1; ๐ ≤ ๐; ๐ + +){
for (๐ = 1; ๐ 2 ≤ ๐; ๐ + +){
for (๐ = 1; ๐ ≤ ๐; ๐ + +){
printf (“Salam”);
for (๐ = 1; ๐ ≤ 100; ๐ + +){
}
printf (“Salam”);
}}}
}
13
Exercise 6
A(n)
{
int ๐, ๐, ๐, ๐;
for (๐ = 1; ๐ ≤ ๐; ๐ + +){
for (๐ = 1; ๐ ≤ ๐ 2 ; ๐ + +){
for (๐ = 1; ๐ ≤
๐
;๐
2
+ +){
printf (“Salam”);
}}}
}
15
Alg 7(n)
Exercise 7 &amp; 8
Alg 8(n)
{
int ๐ = 1, ๐  = 1;
{
int ๐ = 1, ๐  = 1;
while (๐  ≤ ๐){
while (๐  ≤ ๐){
๐ + +;
๐ + +;
๐  + +;
๐  = ๐  + ๐;
printf (“Salam”);
printf (“Salam”);
}}
}}
17
```