MADChap3 Algorithms
MADChap3 Algorithms
Algorithms
MAD101
Ly Anh Duong
duongla3@fe.edu.vn
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.
• Input (Đầu vào). An algorithm has input values from a specified set.
• Output (Đầu ra). From each set of input values an algorithm produces
output values from a specified set (solution).
• Definiteness (xác định). The steps of an algorithm must be defined
precisely.
• Correctness (chính xác). An algorithm should produce the correct output
values for each set of input values.
• Finiteness (hữu hạn). An algorithm should produce the desired output
after a finite (but perhaps large) number of steps for any input in the set.
• Effectiveness (hiệu quả). It must be possible to perform each step of an
algorithm exactly and in a finite amount of time.
• Generality (tổng quát). The procedure should be applicable for all
problems of the desired form.
Ly Anh Duong Chapter 3 Algorithms duongla3@fe.edu.vn 5 / 52
Finding the Maximum Element in a Finite
Sequence.
1 Algorithms
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 duongla3@fe.edu.vn 6 / 52
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 duongla3@fe.edu.vn 11 / 52
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 duongla3@fe.edu.vn 12 / 52
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 > 10 → 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
19 = 19 → location = 14
Ly Anh Duong Chapter 3 Algorithms duongla3@fe.edu.vn 13 / 52
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
Example.
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 duongla3@fe.edu.vn 31 / 52
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 duongla3@fe.edu.vn 32 / 52
Quizz
2 The Growth of Functions
Ans: a
Ly Anh Duong Chapter 3 Algorithms duongla3@fe.edu.vn 33 / 52
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)
Ans: a (f (n) = n)
Ly Anh Duong Chapter 3 Algorithms duongla3@fe.edu.vn 43 / 52
Table of Contents
4 Problems
▶ Algorithms
▶ Complexity of Algorithms
▶ Problems
Algorithms
4 Problems