Professional Documents
Culture Documents
Lecture 2
Preliminaries
Algorithmic Notation
Recap
An algorithm is a finite step-by-step list of well-defined instructions
for solving a particular problem.
I.
II.
Algorithmic Notation
Example: An array DATA of numerical values is in memory.We want to
find the location LOC and the value MAX of the largest element of
DATA.
Algorithmic Notation
Conventions to be followed in algorithms in this course.
Identifying Number
Steps, Control, Exit
Comments
Variable names and Indexes
Input and Output
Procedures (sub-algorithm)
Control Structures
According to Bhm and Jacopini's research,
Algorithm and their equivalent programs could be written in
terms of mainly three control structures, namely;
I.
Sequence structure/logic, or sequential flow
II.
Selection structure/logic, or conditional flow
III.
Iteration structure/logic, or Repetitive flow
Sequence logic (Sequential flow)
Built into C++.
Unless directed otherwise, the computer executes C++ statements
one after the other in the order in which they are written i.e. in
sequence
Control Structures
Selection logic (Conditional flow)
Employs a number of conditions.
Flow of control transfers to one of the true condition.
C++ provides three types of selection statements;
if , if-else and switch.
Iteration logic (Repetitive flow)
Enable programs to perform statements repeatedly as long as a
condition remains true.
C++ provides three types of repetition statements
for, while and do-while loops.
Control Structures
Using the while loop instead of a Goto statement, the algorithm 2.1
can be rewritten as follow
Complexity of Algorithms
I.
II.
III.
Analysis of Complexity
Big-O notation
For example,
f(n) = n2+2n+1=O(n2)
where g(n)=n2
Here f(n) is O(g(n)) and is read as f is Big-O of g or Big-O of n2.
Big-O notation
Big-O notation
Big-O notation
The idea in Big O notation isnt to give an actual figure for running
time, but to convey how running times are affected by the number
of items.
Big-O notation
First solve
f(n) = n2+2n+1=O(n2)
where g(n)=n2
n
2 + 2 + 1 2
9/4
2 + 2 + 1
16/9
25/16
36/25
49/36
Big-O notation
As