Professional Documents
Culture Documents
Lesson 2
Lesson 2
Lesson 2
ALGORITHMS
COMPILED BY JN MASI
What does it mean to analyse an algorithm?
• This can mean predicting the resources that the algorithm requires
• Theoretical study of a computer program performance and resource
usage
• These resources can be
• Memory
• Running time
• etc
Input size
• sum = 0;
for (i=1; i<=n; i++)
for (j=1; j<=n; j++)
sum++;
• Drawbacks
• 1. A lot of effort putting the algorithm in code, yet in the
end only 1 will be choosen
• 2. There is a chance 1 of the algorithms was written better
than the other
• 3. The choice of test cases might favor one algorithm,
• 4. You might find out that none of the implemented
algorithms suite your budget
Choosing an efficient algorithm
• Growth rate
This is the rate at which cost of the algorithm
grows as the size of input grows
•a
Ways of analysing an algorithm
• When the upper and lower bounds are the same within a
constant factor, the big theta notation is used to denote
this
• For example if the upper bound is T(n)= O(f(n)) and the
lower bound is T(n)=Ω(f(n))
• This means Ω(f(n))= O(f(n))
• Therefore T(n)= Θ(f(n))
Theta notation Θ
• Ex 1
• Int Num = 2
• Ex 2
• sum = 0;
for (i=1; i<=n; i++)
sum += n;
Calculating the run time of an algorithm
• Ex 3
sum = 0;
for (j=1; j<=n; j++)
for (i=1; i<=j; i++)
sum++;
for (k=0; k<n; k++)
A[k] = k;
Calculating the run time of an algorithm
• Ex 4
sum1 = 0;
for (i=1; i<=n; i++)
for (j=1; j<=n; j++)
sum1++;
sum2 = 0;
for (i=1; i<=n; i++)
for (j=1; j<=i; j++)
sum2++;
QUESTIONS???