MADChap3 Algorithms
MADChap3 Algorithms
Algorithms
MAD101
Ly Anh Duong
[email protected]
Table of Contents
1 Algorithms
▶ Algorithms
▶ Complexity of Algorithms
▶ Problems
Example.
1 Algorithms
An algorithm is a finite sequence of precise instructions (mã lệnh xác định) for
performing a computation or for solving a problem.
Example. Finding the Maximum Element in a Finite Sequence {8, 4, 11, 3, 10}.
procedure max (a1 , a2 , a3 , a4 , a5 : integers)
max:=a1
for i:=2 to 5
if max < ai then max:= ai
return max {max is the largest element}
Ly Anh Duong Chapter 3 Algorithms [email protected] 6 / 54
Example.
1 Algorithms
Example. List all the steps used to search for 9 in the sequence 2, 3, 4, 5, 6, 8, 9, 11
using a linear search. How many comparisons required to search for 9 in the
sequence.
Example. To search for 19 in the list 1, 2, 3, 5, 6, 7, 8, 10, 12, 13, 15, 16, 18, 19, 20, 22
Ly Anh Duong Chapter 3 Algorithms [email protected] 11 / 54
Solution
1 Algorithms
1, 2, 3, 5, 6, 7, 8, 10, 12, 13, 15, 16, 18, 19, 20, 22. Search 19
procedure binary search ( x:integer, a1 , a2 , . . . , a16 : increasing integers)
i := 1 { i is left endpoint of search interval}
j := 16 { j is right endpoint of search interval}
while i < j
m := ⌊(i + j)/2⌋
if x > am then i := m + 1
else j := m
if x = ai then location: = i
else location: = 0
return location {location is the subscript of the term that equals x, or is 0 if
x is not found}
Ly Anh Duong Chapter 3 Algorithms [email protected] 12 / 54
1, 2, 3, 5, 6, 7, 8, 10, 12, 13, 15, 16, 18, 19, 20, 22. Search
19
1 Algorithms
i=1
j = 16
while i < j
1 + 16
m=⌊ ⌋ = ⌊8.5⌋ = 8
2
19 > 16 → i = 8 + 1 = 9, 19 ̸= 12 →location:=0
9 + 16
m=⌊ ⌋ = ⌊12.5⌋ = 12
2
19 > 16 → i = 12 + 1 = 13, 19 ̸= 18 →location:=0
13 + 16
m=⌊ ⌋ = ⌊14.5⌋ = 14
2
19 ≤ 19 → j = 14, 19 ̸= 18 →location:=0
13 + 14
m=⌊ ⌋ = ⌊13.5⌋ = 13
2
19 > 18 → i = 13 + 1 = 14
19Anh
Ly = 19 → location
Duong = 14 Chapter 3 Algorithms [email protected] 13 / 54
Sorting
1 Algorithms
Sorting is putting the elements into a list in which the elements are in increasing
order.
For instance, sorting the list 7, 2, 1, 4, 5, 9 produces the list 1, 2, 4, 5, 7, 9. Sorting
the list d, h, c, a, f (using alphabetical order) produces the list a, c, d, f, h.
j=4
i = 1, 1 ≤ 2 → i = 1, m = 1
k = 0 → a4 = 4
k = 1 → a3 = 3
k = 2 → a2 = 2
a1 = 1
→ 1, 2, 3, 4, 5
j=5
i = 1, 5 > 1
i = 2, 5 > 2
i = 3, 5 > 3
i = 4, 5 > 4
Thus, → 1, 2, 3, 4, 5
▶ Algorithms
▶ Complexity of Algorithms
▶ Problems
Introduction
2 The Growth of Functions
COROLLARY. Suppose that f1 (x) and f2 (x) are both O(g(x)). Then
(f1 + f2 )(x) is O(g(x)).
Ans: C
Ly Anh Duong Chapter 3 Algorithms [email protected] 33 / 54
Quizz
2 The Growth of Functions
Ans: a,b (hàm nào có O(n))(f (x) = O(g(x)) mean f(x) is big-oh of g(x))
Ly Anh Duong Chapter 3 Algorithms [email protected] 34 / 54
Quizz
2 The Growth of Functions
Ans: a
Ly Anh Duong Chapter 3 Algorithms [email protected] 35 / 54
Big-Omega and Big-Theta Notation
2 The Growth of Functions
▶ Algorithms
▶ Complexity of Algorithms
▶ Problems
Introduction
3 Complexity of Algorithms
Describe the time complexity (Worst-case) of the algorithm for finding the largest
element in a set.
Ans:f(n)=n+n=2n is O(n)
▶ Algorithms
▶ Complexity of Algorithms
▶ Problems
Algorithms
4 Problems