Dayschool 2
Dayschool 2
Dayschool 2
Day School – 2
by
Shalini Rajasingham
Adapted from
Dr Gehan Anthonys
Asymptotic Analysis:
• When analyzing in terms of time and space, we cannot provide an
exact number to define the time and space required by the algorithm;
• Thus, it is expressed using some standard notations, known
as asymptotic notations. The term ”Asymptotic“ means approaching
a value or curve arbitrarily closely.
T(n)
n
where f(n) and g(n) are operations of an algorithm with the number of input data n and k is a positive constant.
6
August 13, 2024 EEX4465 -- Data Structures and Algorithms
Big O - Notation
Definition:
Assignment (s 1)
Addition (s + 1) O(1)
Multiplication (s*2)
Comparison (s < 10)
Solution:
If n is even If n is odd
f(n) O(n4) f(n) O(n2)
g(n) O(n2) g(n) O(n3)
Thus,
T1(n) + T2(n) O(n4) T1(n) + T2(n) O(n3)
E.g., State the efficiency of the following algorithm w.r.t “printf” statement.
scanf("%d",&n);
for ( i = 0; i < n; i++)
for ( j = 0; j < n; j++)
printf("(%d,%d) \n", i, j);
Solution:
Exercises
Solution:
We can find the running time as follow:
Note:
• Best, worst, and average are difficult to deal with precisely because
the details are very complicated;
• It easier to talk about upper and lower bounds of the function;
• Asymptotic notations (Ω; Ѳ; O) are also can be used for practically
deal with complexity functions.
… few minutes.
Structures
• For each procedure or subroutine
BEGIN name
END name
• For single selection
IF condition THEN
statements
ELSE
statements
ENDIF
August 13, 2024 EEX4435/ EEX4465 -- Data Structures and Algorithms 22
Structures
• For multiway selection
CASEWHERE expression evaluates to
A: process A
B: process B
………
OTHERWISE process alternative
ENDCASE
• For pre test repetition • For post test repetition
• For Loop
FOR variable = start TO finish STEP increments
statements
NEXT variable
or
Solution:
IF CA mark >= 40 THEN
Display “Eligible to sit the final exam”
ELSE
Display “Not eligible to sit the final exam”
ENDIF