Data - Structure Lecture - 2
Data - Structure Lecture - 2
and Algorithms
Lecture 2
• after an algorithm is designed it
should also be analyzed.
Algorithm • we usually count the number of
operations performed by the
Analysis: algorithm.
The Big-O • not on the actual computer time to
Notation execute the algorithm. This is
because a particular algorithm can
be implemented on a variety of
computers and the speed of the
computer can affect the execution
time.
Example 1
• Line 1 has one operation-Line 2 has two operations- Line 3 has one
operation-Line4 has one operation-Line 6 has one operation-and Line 7 has
three operations. Either Line 4 or Line 6 executes. Therefore, the total
number of operations executed in the preceding code is1+2+1+1+3= 8.
Example 2
This algorithm has five operations (Lines 1 through 4) before the while loop.
there are nine or eight operations after the while loop ,
If the while loop executes 10 times, the total number of operations executed is:
(10*5 + 1)+ 5+9 or (10*5 + 1)+ 5+8
At n times, 5n+15 or 5n+14
• Suppose that an algorithm performs f(n) basic operations to accomplish a task
• the computer time it would take to execute f(n) operations is cf(n).
• c depends on the speed of the computer