CNG315_Lecture1_Introduction-to-Algorithms
CNG315_Lecture1_Introduction-to-Algorithms
Introduction to Algorithms
Lecture 1 1
Algorithms
Lecture 1 2
Algorithm as a tool for solving a well-
specified computational problem
• Sorting Problem
32 41 59 26 43 58 5
5 26 32 41 43 58 59
Lecture 1 3
Algorithms
Lecture 1 5
Designing algorithms
• It must be correct
• It must terminate
• It must be efficient
– Design algorithms which minimise the cost!
– Implementations of an algorithm must run as fast
as possible
– How is this measured?
• Running time
Lecture 1 6
Running time of a program
Lecture 1 7
Running time of an algorithm
Lecture 1 8
Describing algorithms
Lecture 1 9
Pseudo-code example
Algorithm arrayMax(A,n):
Input: An array A storing n integers.
Output: The maximum element in A.
currentMax A[0]
for i 1 to n - 1 do
if currentMax < A[i] then
currentMax A[i]
return currentMax
Lecture 1 10
Pseudo-code conventions
Lecture 1 11
Algorithm Structure
statements…
Lecture 1 12
Statements
Lecture 1 13
Control Structures
• Decision structures
if ... then ... [else ...]
• While loops
while ... do …
• Repeat loops
repeat ... until ...
• For loop
for ... do …
Lecture 1 14
Expressions
Lecture 1 15
General rules on pseudo-code
Lecture 1 16
Back to running time
Lecture 1 17
The problem of sorting
• Example Case:
– Input: 8 2 4 9 3 6
– Output: 2 3 4 6 8 9
Lecture 1 18
Insertion Sort
1 i j n
A:
key
sorted
Lecture 1 19
Example of Insertion Sort
8 2 4 9 3 6
Lecture 1 20
Example of Insertion Sort
8 2 4 9 3 6
Lecture 1 21
Example of Insertion Sort
8 2 4 9 3 6
2 8 4 9 3 6
Lecture 1 22
Example of Insertion Sort
8 2 4 9 3 6
2 8 4 9 3 6
Lecture 1 23
Example of Insertion Sort
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
Lecture 1 24
Example of Insertion Sort
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
Lecture 1 25
Example of Insertion Sort
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
Lecture 1 26
Example of Insertion Sort
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
Lecture 1 27
Example of Insertion Sort
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
2 3 4 8 9 6
Lecture 1 28
Example of Insertion Sort
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
2 3 4 8 9 6
2 3 4 6 8 9 done
Lecture 1 29
Running time of Insertion Sort
Lecture 1 30
Kinds of running time analyses
• Best-case: (NEVER)
– Cheat with a slow algorithm that works fast on
some input.
• Average-case: (Sometimes)
– T(n) = expected time of algorithm over all inputs
of size n.
– Need assumption of statistical distribution of
inputs.
• Worst-case: (Usually)
– T(n) = maximum time of algorithm on any input of
size n.
– Generally, we seek upper bounds on the running
Lecture 1 31
time, to have a guarantee of performance.
Running time of Insertion Sort
Lecture 1 32
Additional Note: Sigma Symbol
Lecture 1 33
Additional Note: Sigma Symbol
• Useful formulas!
𝑛
∑ 0=0
𝑖 =1
∑ 𝑐=𝑐 ∗ 𝑛
𝑖=1
𝑛
𝑛 ∗(𝑛+1)
∑ 𝑖= 2
𝑖 =1
Lecture 1 34
Running time of Insertion Sort
Lecture 1 36
Running time of Insertion Sort
Lecture 1 37
Running time of Insertion Sort
• Running time is
Lecture 1 38