Algorithms The Growth of Functions Complexity of Algorithms Problems
Chapter 3. Algorithms
Lai Văn Phút
Ngày 8 tháng 6 năm 2022
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Contents
1. Algorithms
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Contents
1. Algorithms
2. The Growth of Functions
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Contents
1. Algorithms
2. The Growth of Functions
3. Complexity of Algorithms
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Example.
• Finding the Maximum Element in a Finite Sequence
{8, 4, 11, 3, 10}.
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Example.
• Finding the Maximum Element in a Finite Sequence
{8, 4, 11, 3, 10}.
• To search for 19 in the list
1, 2, 3, 5, 6, 7, 8, 10, 12, 13, 15, 16, 18, 19, 20, 22
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Example.
• Finding the Maximum Element in a Finite Sequence
{8, 4, 11, 3, 10}.
• To search for 19 in the list
1, 2, 3, 5, 6, 7, 8, 10, 12, 13, 15, 16, 18, 19, 20, 22
• ...
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Definition
An algorithm is a finite sequence of precise instructions
(mã lệnh xác định) for performing a computation or for
solving a problem.
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
PROPERTIES OF ALGORITHMS
• Input. An algorithm has input values from a specified
set.
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
PROPERTIES OF ALGORITHMS
• Input. An algorithm has input values from a specified
set.
• Output. From each set of input values an algorithm
produces output values from a specified set (solution).
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
PROPERTIES OF ALGORITHMS
• Input. An algorithm has input values from a specified
set.
• Output. From each set of input values an algorithm
produces output values from a specified set (solution).
• Definiteness. The steps of an algorithm must be
defined precisely.
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
PROPERTIES OF ALGORITHMS
• Input. An algorithm has input values from a specified
set.
• Output. From each set of input values an algorithm
produces output values from a specified set (solution).
• Definiteness. The steps of an algorithm must be
defined precisely.
• Correctness. An algorithm should produce the correct
output values for each set of input values.
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
PROPERTIES OF ALGORITHMS
• Input. An algorithm has input values from a specified
set.
• Output. From each set of input values an algorithm
produces output values from a specified set (solution).
• Definiteness. The steps of an algorithm must be
defined precisely.
• Correctness. An algorithm should produce the correct
output values for each set of input values.
• Finiteness. An algorithm should produce the desired
output after a finite (but perhaps large) number of
steps for any input in the set.
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
PROPERTIES OF ALGORITHMS
• Input. An algorithm has input values from a specified
set.
• Output. From each set of input values an algorithm
produces output values from a specified set (solution).
• Definiteness. The steps of an algorithm must be
defined precisely.
• Correctness. An algorithm should produce the correct
output values for each set of input values.
• Finiteness. An algorithm should produce the desired
output after a finite (but perhaps large) number of
steps for any input in the set.
• Effectiveness. It must be possible to perform each step
of an algorithm exactly and in a finite amount of time.
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
PROPERTIES OF ALGORITHMS
• Input. An algorithm has input values from a specified
set.
• Output. From each set of input values an algorithm
produces output values from a specified set (solution).
• Definiteness. The steps of an algorithm must be
defined precisely.
• Correctness. An algorithm should produce the correct
output values for each set of input values.
• Finiteness. An algorithm should produce the desired
output after a finite (but perhaps large) number of
steps for any input in the set.
• Effectiveness. It must be possible to perform each step
of an algorithm exactly and in a finite amount of time.
• Generality. The procedure should be applicable for all
problems of the desired form.
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Finding the Maximum Element in a Finite
Sequence.
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Finding the Maximum Element in a Finite
Sequence.
Example. Finding the Maximum Element in a Finite
Sequence {8, 4, 11, 3, 10}.
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Finding the Maximum Element in a Finite
Sequence.
Example. Finding the Maximum Element in a Finite
Sequence {8, 4, 11, 3, 10}.
procedure max (a1 , a2 , a3 , a4 , a5 : integers)
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Finding the Maximum Element in a Finite
Sequence.
Example. Finding the Maximum Element in a Finite
Sequence {8, 4, 11, 3, 10}.
procedure max (a1 , a2 , a3 , a4 , a5 : integers)
max:=a1
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Finding the Maximum Element in a Finite
Sequence.
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
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Finding the Maximum Element in a Finite
Sequence.
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
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Finding the Maximum Element in a Finite
Sequence.
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}
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Example.
{8, 4, 11, 3, 10}
Max=8
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Example.
{8, 4, 11, 3, 10}
Max=8
i = 2(8 ≥ 4) → Max=8
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Example.
{8, 4, 11, 3, 10}
Max=8
i = 2(8 ≥ 4) → Max=8
i = 3(8 < 11) → Max=11
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Example.
{8, 4, 11, 3, 10}
Max=8
i = 2(8 ≥ 4) → Max=8
i = 3(8 < 11) → Max=11
i = 4(11 ≥ 3) → Max=11
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Example.
{8, 4, 11, 3, 10}
Max=8
i = 2(8 ≥ 4) → Max=8
i = 3(8 < 11) → Max=11
i = 4(11 ≥ 3) → Max=11
i = 5(11 ≥ 10) → Max=11
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Example.
{8, 4, 11, 3, 10}
Max=8
i = 2(8 ≥ 4) → Max=8
i = 3(8 < 11) → Max=11
i = 4(11 ≥ 3) → Max=11
i = 5(11 ≥ 10) → Max=11
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Example.
{8, 4, 11, 3, 10}
Max=8
i = 2(8 ≥ 4) → Max=8
i = 3(8 < 11) → Max=11
i = 4(11 ≥ 3) → Max=11
i = 5(11 ≥ 10) → Max=11
Thus, Max=11
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
The Linear Search Algorithm
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
The Linear Search Algorithm
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.
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Solution
Below is the linear search algorithm in pseudocode
2, 3, 4, 5, 6, 8, 9, 11
procedure linear search (x: integer, a1 , a2 , ..., a8 :
distinct integers)
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Solution
Below is the linear search algorithm in pseudocode
2, 3, 4, 5, 6, 8, 9, 11
procedure linear search (x: integer, a1 , a2 , ..., a8 :
distinct integers)
i := 1
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Solution
Below is the linear search algorithm in pseudocode
2, 3, 4, 5, 6, 8, 9, 11
procedure linear search (x: integer, a1 , a2 , ..., a8 :
distinct integers)
i := 1
while (i ≤ 8 and x 6= ai )
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Solution
Below is the linear search algorithm in pseudocode
2, 3, 4, 5, 6, 8, 9, 11
procedure linear search (x: integer, a1 , a2 , ..., a8 :
distinct integers)
i := 1
while (i ≤ 8 and x 6= ai )
i := i + 1
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Solution
Below is the linear search algorithm in pseudocode
2, 3, 4, 5, 6, 8, 9, 11
procedure linear search (x: integer, a1 , a2 , ..., a8 :
distinct integers)
i := 1
while (i ≤ 8 and x 6= ai )
i := i + 1
if i ≤ 8 then location: = i
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Solution
Below is the linear search algorithm in pseudocode
2, 3, 4, 5, 6, 8, 9, 11
procedure linear search (x: integer, a1 , a2 , ..., a8 :
distinct integers)
i := 1
while (i ≤ 8 and x 6= ai )
i := i + 1
if i ≤ 8 then location: = i
else location: = 0
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Solution
Below is the linear search algorithm in pseudocode
2, 3, 4, 5, 6, 8, 9, 11
procedure linear search (x: integer, a1 , a2 , ..., a8 :
distinct integers)
i := 1
while (i ≤ 8 and x 6= ai )
i := i + 1
if i ≤ 8 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}
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
2, 3, 4, 5, 6, 8, 9, 11
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
The Binary(nhị phân) Search Algorithm
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
The Binary(nhị phân) Search Algorithm
Example. To search for 19 in the list
1, 2, 3, 5, 6, 7, 8, 10, 12, 13, 15, 16, 18, 19, 20, 22
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Solution
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)
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Solution
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}
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Solution
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}
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Solution
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
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Solution
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 := b(i + j )/2c
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Solution
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 := b(i + j )/2c
if x > am then i := m + 1
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Solution
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 := b(i + j )/2c
if x > am then i := m + 1
else j := m
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Solution
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 := b(i + j )/2c
if x > am then i := m + 1
else j := m
if x = ai then location: = i
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Solution
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 := b(i + j )/2c
if x > am then i := m + 1
else j := m
if x = ai then location: = i
else location: = 0
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Solution
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 := b(i + j )/2c
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}
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
1, 2, 3, 5, 6, 7, 8, 10, 12, 13, 15, 16, 18, 19, 20, 22.
Search 19
i=1
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
1, 2, 3, 5, 6, 7, 8, 10, 12, 13, 15, 16, 18, 19, 20, 22.
Search 19
i=1
j = 16
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
1, 2, 3, 5, 6, 7, 8, 10, 12, 13, 15, 16, 18, 19, 20, 22.
Search 19
i=1
j = 16
while i < j
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
1, 2, 3, 5, 6, 7, 8, 10, 12, 13, 15, 16, 18, 19, 20, 22.
Search 19
i=1
j = 16
while i < j
1 + 16
m=b c = b8.5c = 8
2
19 > 16 → i = 8 + 1 = 9,
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
1, 2, 3, 5, 6, 7, 8, 10, 12, 13, 15, 16, 18, 19, 20, 22.
Search 19
i=1
j = 16
while i < j
1 + 16
m=b c = b8.5c = 8
2
19 > 16 → i = 8 + 1 = 9,
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
1, 2, 3, 5, 6, 7, 8, 10, 12, 13, 15, 16, 18, 19, 20, 22.
Search 19
i=1
j = 16
while i < j
1 + 16
m=b c = b8.5c = 8
2
19 > 16 → i = 8 + 1 = 9, 19 6= 12 →location:=0
9 + 16
m=b c = b12.5c = 12
2
19 > 16 → i = 12 + 1 = 13,
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
1, 2, 3, 5, 6, 7, 8, 10, 12, 13, 15, 16, 18, 19, 20, 22.
Search 19
i=1
j = 16
while i < j
1 + 16
m=b c = b8.5c = 8
2
19 > 16 → i = 8 + 1 = 9, 19 6= 12 →location:=0
9 + 16
m=b c = b12.5c = 12
2
19 > 16 → i = 12 + 1 = 13,
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
1, 2, 3, 5, 6, 7, 8, 10, 12, 13, 15, 16, 18, 19, 20, 22.
Search 19
i=1
j = 16
while i < j
1 + 16
m=b c = b8.5c = 8
2
19 > 16 → i = 8 + 1 = 9, 19 6= 12 →location:=0
9 + 16
m=b c = b12.5c = 12
2
19 > 16 → i = 12 + 1 = 13, 19 6= 18 →location:=0
13 + 16
m=b c = b14.5c = 14
2
19 ≤ 19 → j = 14,
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
1, 2, 3, 5, 6, 7, 8, 10, 12, 13, 15, 16, 18, 19, 20, 22.
Search 19
i=1
j = 16
while i < j
1 + 16
m=b c = b8.5c = 8
2
19 > 16 → i = 8 + 1 = 9, 19 6= 12 →location:=0
9 + 16
m=b c = b12.5c = 12
2
19 > 16 → i = 12 + 1 = 13, 19 6= 18 →location:=0
13 + 16
m=b c = b14.5c = 14
2
19 ≤ 19 → j = 14,
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
1, 2, 3, 5, 6, 7, 8, 10, 12, 13, 15, 16, 18, 19, 20, 22.
Search 19
i=1
j = 16
while i < j
1 + 16
m=b c = b8.5c = 8
2
19 > 16 → i = 8 + 1 = 9, 19 6= 12 →location:=0
9 + 16
m=b c = b12.5c = 12
2
19 > 16 → i = 12 + 1 = 13, 19 6= 18 →location:=0
13 + 16
m=b c = b14.5c = 14
2
19 ≤ 19 → j = 14, 19 6= 18 →location:=0
13 + 14
m=b c = b13.5c = 13
2
19 > 18 → i = 13 + 1 = 14
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
1, 2, 3, 5, 6, 7, 8, 10, 12, 13, 15, 16, 18, 19, 20, 22.
Search 19
i=1
j = 16
while i < j
1 + 16
m=b c = b8.5c = 8
2
19 > 16 → i = 8 + 1 = 9, 19 6= 12 →location:=0
9 + 16
m=b c = b12.5c = 12
2
19 > 16 → i = 12 + 1 = 13, 19 6= 18 →location:=0
13 + 16
m=b c = b14.5c = 14
2
19 ≤ 19 → j = 14, 19 6= 18 →location:=0
13 + 14
m=b c = b13.5c = 13
2
19 > 18 → i = 13 + 1 = 14
19 = 19 → location = 14
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Sorting
Sorting is putting the elements into a list in which the
elements are in increasing order.
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Sorting
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.
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
The Bubble Sort
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
The Bubble Sort
Example. Use the bubble sort to put 3, 2, 4, 1, 5 into
increasing order.
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
The Bubble Sort
Example. Use the bubble sort to put 3, 2, 4, 1, 5 into
increasing order.
procedure bubble sort (a1 , a2 , . . . , a5 :real numbers
with 5 ≥ 2)
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
The Bubble Sort
Example. Use the bubble sort to put 3, 2, 4, 1, 5 into
increasing order.
procedure bubble sort (a1 , a2 , . . . , a5 :real numbers
with 5 ≥ 2)
for i := 1 to 4
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
The Bubble Sort
Example. Use the bubble sort to put 3, 2, 4, 1, 5 into
increasing order.
procedure bubble sort (a1 , a2 , . . . , a5 :real numbers
with 5 ≥ 2)
for i := 1 to 4
for j := 1 to 5 − i
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
The Bubble Sort
Example. Use the bubble sort to put 3, 2, 4, 1, 5 into
increasing order.
procedure bubble sort (a1 , a2 , . . . , a5 :real numbers
with 5 ≥ 2)
for i := 1 to 4
for j := 1 to 5 − i
if aj > aj +1 then interchange aj and aj +1
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
The Bubble Sort
Example. Use the bubble sort to put 3, 2, 4, 1, 5 into
increasing order.
procedure bubble sort (a1 , a2 , . . . , a5 :real numbers
with 5 ≥ 2)
for i := 1 to 4
for j := 1 to 5 − i
if aj > aj +1 then interchange aj and aj +1
{a1 , a2 , . . . , a5 is in increasing order}
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Use the bubble sort to put 3, 2, 4, 1, 5 into
increasing order
i=1
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Use the bubble sort to put 3, 2, 4, 1, 5 into
increasing order
i=1
j = 1 : 3 > 2 → 2, 3, 4, 1, 5
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Use the bubble sort to put 3, 2, 4, 1, 5 into
increasing order
i=1
j = 1 : 3 > 2 → 2, 3, 4, 1, 5
j = 2 : 3 ≤ 4 → 2, 3, 4, 1, 5
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Use the bubble sort to put 3, 2, 4, 1, 5 into
increasing order
i=1
j = 1 : 3 > 2 → 2, 3, 4, 1, 5
j = 2 : 3 ≤ 4 → 2, 3, 4, 1, 5
j = 3 : 4 > 1 → 2, 3, 1, 4, 5
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Use the bubble sort to put 3, 2, 4, 1, 5 into
increasing order
i=1
j = 1 : 3 > 2 → 2, 3, 4, 1, 5
j = 2 : 3 ≤ 4 → 2, 3, 4, 1, 5
j = 3 : 4 > 1 → 2, 3, 1, 4, 5
j = 4 : 4 ≤ 5 → 2, 3, 1, 4, 5
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Use the bubble sort to put 3, 2, 4, 1, 5 into
increasing order
i=1
j = 1 : 3 > 2 → 2, 3, 4, 1, 5
j = 2 : 3 ≤ 4 → 2, 3, 4, 1, 5
j = 3 : 4 > 1 → 2, 3, 1, 4, 5
j = 4 : 4 ≤ 5 → 2, 3, 1, 4, 5
i=2
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Use the bubble sort to put 3, 2, 4, 1, 5 into
increasing order
i=1
j = 1 : 3 > 2 → 2, 3, 4, 1, 5
j = 2 : 3 ≤ 4 → 2, 3, 4, 1, 5
j = 3 : 4 > 1 → 2, 3, 1, 4, 5
j = 4 : 4 ≤ 5 → 2, 3, 1, 4, 5
i=2
j = 1 : 2 ≤ 3 → 2, 3, 1, 4, 5
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Use the bubble sort to put 3, 2, 4, 1, 5 into
increasing order
i=1
j = 1 : 3 > 2 → 2, 3, 4, 1, 5
j = 2 : 3 ≤ 4 → 2, 3, 4, 1, 5
j = 3 : 4 > 1 → 2, 3, 1, 4, 5
j = 4 : 4 ≤ 5 → 2, 3, 1, 4, 5
i=2
j = 1 : 2 ≤ 3 → 2, 3, 1, 4, 5
j = 2 : 3 > 1 → 2, 1, 3, 4, 5
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Use the bubble sort to put 3, 2, 4, 1, 5 into
increasing order
i=1
j = 1 : 3 > 2 → 2, 3, 4, 1, 5
j = 2 : 3 ≤ 4 → 2, 3, 4, 1, 5
j = 3 : 4 > 1 → 2, 3, 1, 4, 5
j = 4 : 4 ≤ 5 → 2, 3, 1, 4, 5
i=2
j = 1 : 2 ≤ 3 → 2, 3, 1, 4, 5
j = 2 : 3 > 1 → 2, 1, 3, 4, 5
j = 3 : 3 ≤ 4 → 2, 1, 3, 4, 5
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Use the bubble sort to put 3, 2, 4, 1, 5 into
increasing order
i=1
j = 1 : 3 > 2 → 2, 3, 4, 1, 5
j = 2 : 3 ≤ 4 → 2, 3, 4, 1, 5
j = 3 : 4 > 1 → 2, 3, 1, 4, 5
j = 4 : 4 ≤ 5 → 2, 3, 1, 4, 5
i=2
j = 1 : 2 ≤ 3 → 2, 3, 1, 4, 5
j = 2 : 3 > 1 → 2, 1, 3, 4, 5
j = 3 : 3 ≤ 4 → 2, 1, 3, 4, 5
i=3
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Use the bubble sort to put 3, 2, 4, 1, 5 into
increasing order
i=1
j = 1 : 3 > 2 → 2, 3, 4, 1, 5
j = 2 : 3 ≤ 4 → 2, 3, 4, 1, 5
j = 3 : 4 > 1 → 2, 3, 1, 4, 5
j = 4 : 4 ≤ 5 → 2, 3, 1, 4, 5
i=2
j = 1 : 2 ≤ 3 → 2, 3, 1, 4, 5
j = 2 : 3 > 1 → 2, 1, 3, 4, 5
j = 3 : 3 ≤ 4 → 2, 1, 3, 4, 5
i=3
j = 1 : 2 > 1 → 1, 2, 3, 4, 5
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Use the bubble sort to put 3, 2, 4, 1, 5 into
increasing order
i=1
j = 1 : 3 > 2 → 2, 3, 4, 1, 5
j = 2 : 3 ≤ 4 → 2, 3, 4, 1, 5
j = 3 : 4 > 1 → 2, 3, 1, 4, 5
j = 4 : 4 ≤ 5 → 2, 3, 1, 4, 5
i=2
j = 1 : 2 ≤ 3 → 2, 3, 1, 4, 5
j = 2 : 3 > 1 → 2, 1, 3, 4, 5
j = 3 : 3 ≤ 4 → 2, 1, 3, 4, 5
i=3
j = 1 : 2 > 1 → 1, 2, 3, 4, 5
j = 2 : 2 ≤ 3 → 1, 2, 3, 4, 5
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Use the bubble sort to put 3, 2, 4, 1, 5 into
increasing order
i=1
j = 1 : 3 > 2 → 2, 3, 4, 1, 5
j = 2 : 3 ≤ 4 → 2, 3, 4, 1, 5
j = 3 : 4 > 1 → 2, 3, 1, 4, 5
j = 4 : 4 ≤ 5 → 2, 3, 1, 4, 5
i=2
j = 1 : 2 ≤ 3 → 2, 3, 1, 4, 5
j = 2 : 3 > 1 → 2, 1, 3, 4, 5
j = 3 : 3 ≤ 4 → 2, 1, 3, 4, 5
i=3
j = 1 : 2 > 1 → 1, 2, 3, 4, 5
j = 2 : 2 ≤ 3 → 1, 2, 3, 4, 5
i=4
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Use the bubble sort to put 3, 2, 4, 1, 5 into
increasing order
i=1
j = 1 : 3 > 2 → 2, 3, 4, 1, 5
j = 2 : 3 ≤ 4 → 2, 3, 4, 1, 5
j = 3 : 4 > 1 → 2, 3, 1, 4, 5
j = 4 : 4 ≤ 5 → 2, 3, 1, 4, 5
i=2
j = 1 : 2 ≤ 3 → 2, 3, 1, 4, 5
j = 2 : 3 > 1 → 2, 1, 3, 4, 5
j = 3 : 3 ≤ 4 → 2, 1, 3, 4, 5
i=3
j = 1 : 2 > 1 → 1, 2, 3, 4, 5
j = 2 : 2 ≤ 3 → 1, 2, 3, 4, 5
i=4
j = 1 : 1 ≤ 2 → 1, 2, 3, 4, 5
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Use the bubble sort to put 3, 2, 4, 1, 5 into
increasing order
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
The Insertion Sort
(i < j, while True->compute, False->next)
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
The Insertion Sort
(i < j, while True->compute, False->next)
Example. Use the insertion sort to put the elements of the
list 3, 2, 4, 1, 5 in increasing order.
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Use the insertion sort to put the elements of the list
3, 2, 4, 1, 5 in increasing order
procedure insertion sort (a1 , a2 , . . . , a5 :real numbers
with 5 ≥ 2)
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Use the insertion sort to put the elements of the list
3, 2, 4, 1, 5 in increasing order
procedure insertion sort (a1 , a2 , . . . , a5 :real numbers
with 5 ≥ 2)
for j := 2 to 5 { j: position of the examined element }
{ finding out the right position of aj }
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Use the insertion sort to put the elements of the list
3, 2, 4, 1, 5 in increasing order
procedure insertion sort (a1 , a2 , . . . , a5 :real numbers
with 5 ≥ 2)
for j := 2 to 5 { j: position of the examined element }
{ finding out the right position of aj }
i := 1
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Use the insertion sort to put the elements of the list
3, 2, 4, 1, 5 in increasing order
procedure insertion sort (a1 , a2 , . . . , a5 :real numbers
with 5 ≥ 2)
for j := 2 to 5 { j: position of the examined element }
{ finding out the right position of aj }
i := 1
while aj > ai
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Use the insertion sort to put the elements of the list
3, 2, 4, 1, 5 in increasing order
procedure insertion sort (a1 , a2 , . . . , a5 :real numbers
with 5 ≥ 2)
for j := 2 to 5 { j: position of the examined element }
{ finding out the right position of aj }
i := 1
while aj > ai
i := i + 1
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Use the insertion sort to put the elements of the list
3, 2, 4, 1, 5 in increasing order
procedure insertion sort (a1 , a2 , . . . , a5 :real numbers
with 5 ≥ 2)
for j := 2 to 5 { j: position of the examined element }
{ finding out the right position of aj }
i := 1
while aj > ai
i := i + 1
m := aj { save aj }
{ moving j-i elements backward }
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Use the insertion sort to put the elements of the list
3, 2, 4, 1, 5 in increasing order
procedure insertion sort (a1 , a2 , . . . , a5 :real numbers
with 5 ≥ 2)
for j := 2 to 5 { j: position of the examined element }
{ finding out the right position of aj }
i := 1
while aj > ai
i := i + 1
m := aj { save aj }
{ moving j-i elements backward }
for k := 0 to j − i − 1
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Use the insertion sort to put the elements of the list
3, 2, 4, 1, 5 in increasing order
procedure insertion sort (a1 , a2 , . . . , a5 :real numbers
with 5 ≥ 2)
for j := 2 to 5 { j: position of the examined element }
{ finding out the right position of aj }
i := 1
while aj > ai
i := i + 1
m := aj { save aj }
{ moving j-i elements backward }
for k := 0 to j − i − 1
aj −k := aj −k −1
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Use the insertion sort to put the elements of the list
3, 2, 4, 1, 5 in increasing order
procedure insertion sort (a1 , a2 , . . . , a5 :real numbers
with 5 ≥ 2)
for j := 2 to 5 { j: position of the examined element }
{ finding out the right position of aj }
i := 1
while aj > ai
i := i + 1
m := aj { save aj }
{ moving j-i elements backward }
for k := 0 to j − i − 1
aj −k := aj −k −1
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Use the insertion sort to put the elements of the list
3, 2, 4, 1, 5 in increasing order
procedure insertion sort (a1 , a2 , . . . , a5 :real numbers
with 5 ≥ 2)
for j := 2 to 5 { j: position of the examined element }
{ finding out the right position of aj }
i := 1
while aj > ai
i := i + 1
m := aj { save aj }
{ moving j-i elements backward }
for k := 0 to j − i − 1
aj −k := aj −k −1
{move aj to the position i}
ai := m
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Use the insertion sort to put the elements of the list
3, 2, 4, 1, 5 in increasing order
procedure insertion sort (a1 , a2 , . . . , a5 :real numbers
with 5 ≥ 2)
for j := 2 to 5 { j: position of the examined element }
{ finding out the right position of aj }
i := 1
while aj > ai
i := i + 1
m := aj { save aj }
{ moving j-i elements backward }
for k := 0 to j − i − 1
aj −k := aj −k −1
{move aj to the position i}
ai := m
{a1 , a2 , . . . , a5 is increasing order}
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
3, 2, 4, 1, 5
j=2
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
3, 2, 4, 1, 5
j=2
i = 1, 2 ≤ 3 → i = 1, m = 2
k = 0 → a2 = 3, a1 = 2
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
3, 2, 4, 1, 5
j=2
i = 1, 2 ≤ 3 → i = 1, m = 2
k = 0 → a2 = 3, a1 = 2
→ 2, 3, 4, 1, 5
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
3, 2, 4, 1, 5
j=2
i = 1, 2 ≤ 3 → i = 1, m = 2
k = 0 → a2 = 3, a1 = 2
→ 2, 3, 4, 1, 5
j=3
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
3, 2, 4, 1, 5
j=2
i = 1, 2 ≤ 3 → i = 1, m = 2
k = 0 → a2 = 3, a1 = 2
→ 2, 3, 4, 1, 5
j=3
i = 1, 4 > 2
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
3, 2, 4, 1, 5
j=2
i = 1, 2 ≤ 3 → i = 1, m = 2
k = 0 → a2 = 3, a1 = 2
→ 2, 3, 4, 1, 5
j=3
i = 1, 4 > 2
i = 2, 4 > 3
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
3, 2, 4, 1, 5
j=2
i = 1, 2 ≤ 3 → i = 1, m = 2
k = 0 → a2 = 3, a1 = 2
→ 2, 3, 4, 1, 5
j=3
i = 1, 4 > 2
i = 2, 4 > 3
→ 2, 3, 4, 1, 5
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
3, 2, 4, 1, 5
j=2
i = 1, 2 ≤ 3 → i = 1, m = 2
k = 0 → a2 = 3, a1 = 2
→ 2, 3, 4, 1, 5
j=3
i = 1, 4 > 2
i = 2, 4 > 3
→ 2, 3, 4, 1, 5
j=4
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
3, 2, 4, 1, 5
j=2
i = 1, 2 ≤ 3 → i = 1, m = 2
k = 0 → a2 = 3, a1 = 2
→ 2, 3, 4, 1, 5
j=3
i = 1, 4 > 2
i = 2, 4 > 3
→ 2, 3, 4, 1, 5
j=4
i = 1, 1 ≤ 2 → i = 1, m = 1
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
3, 2, 4, 1, 5
j=2
i = 1, 2 ≤ 3 → i = 1, m = 2
k = 0 → a2 = 3, a1 = 2
→ 2, 3, 4, 1, 5
j=3
i = 1, 4 > 2
i = 2, 4 > 3
→ 2, 3, 4, 1, 5
j=4
i = 1, 1 ≤ 2 → i = 1, m = 1
k = 0 → a4 = 4
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
3, 2, 4, 1, 5
j=2
i = 1, 2 ≤ 3 → i = 1, m = 2
k = 0 → a2 = 3, a1 = 2
→ 2, 3, 4, 1, 5
j=3
i = 1, 4 > 2
i = 2, 4 > 3
→ 2, 3, 4, 1, 5
j=4
i = 1, 1 ≤ 2 → i = 1, m = 1
k = 0 → a4 = 4
k = 1 → a3 = 3
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
3, 2, 4, 1, 5
j=2
i = 1, 2 ≤ 3 → i = 1, m = 2
k = 0 → a2 = 3, a1 = 2
→ 2, 3, 4, 1, 5
j=3
i = 1, 4 > 2
i = 2, 4 > 3
→ 2, 3, 4, 1, 5
j=4
i = 1, 1 ≤ 2 → i = 1, m = 1
k = 0 → a4 = 4
k = 1 → a3 = 3
k = 2 → a2 = 2
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
3, 2, 4, 1, 5
j=2
i = 1, 2 ≤ 3 → i = 1, m = 2
k = 0 → a2 = 3, a1 = 2
→ 2, 3, 4, 1, 5
j=3
i = 1, 4 > 2
i = 2, 4 > 3
→ 2, 3, 4, 1, 5
j=4
i = 1, 1 ≤ 2 → i = 1, m = 1
k = 0 → a4 = 4
k = 1 → a3 = 3
k = 2 → a2 = 2
a1 = 1
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
3, 2, 4, 1, 5
j=2
i = 1, 2 ≤ 3 → i = 1, m = 2
k = 0 → a2 = 3, a1 = 2
→ 2, 3, 4, 1, 5
j=3
i = 1, 4 > 2
i = 2, 4 > 3
→ 2, 3, 4, 1, 5
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
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
3, 2, 4, 1, 5
j=4
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
3, 2, 4, 1, 5
j=4
i = 1, 1 ≤ 2 → i = 1, m = 1
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
3, 2, 4, 1, 5
j=4
i = 1, 1 ≤ 2 → i = 1, m = 1
k = 0 → a4 = 4
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
3, 2, 4, 1, 5
j=4
i = 1, 1 ≤ 2 → i = 1, m = 1
k = 0 → a4 = 4
k = 1 → a3 = 3
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
3, 2, 4, 1, 5
j=4
i = 1, 1 ≤ 2 → i = 1, m = 1
k = 0 → a4 = 4
k = 1 → a3 = 3
k = 2 → a2 = 2
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
3, 2, 4, 1, 5
j=4
i = 1, 1 ≤ 2 → i = 1, m = 1
k = 0 → a4 = 4
k = 1 → a3 = 3
k = 2 → a2 = 2
a1 = 1
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
3, 2, 4, 1, 5
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
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
3, 2, 4, 1, 5
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
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
3, 2, 4, 1, 5
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
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
3, 2, 4, 1, 5
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
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
3, 2, 4, 1, 5
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
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
3, 2, 4, 1, 5
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
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
3, 2, 4, 1, 5
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
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
3, 2, 4, 1, 5
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
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Introduction
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Example.
(constant that mean f (n) = k, ∀n. In this ex, k = 1)
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Example.
(constant that mean f (n) = k, ∀n. In this ex, k = 1)
→ 1 < log2 n(≡ logn) < n < n2 < 2n < n!
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Example.
(constant that mean f (n) = k, ∀n. In this ex, k = 1)
→ 1 < log2 n(≡ logn) < n < n2 < 2n < n!
• Growth of logn < growth of n
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Example.
(constant that mean f (n) = k, ∀n. In this ex, k = 1)
→ 1 < log2 n(≡ logn) < n < n2 < 2n < n!
• Growth of logn < growth of n
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Example.
(constant that mean f (n) = k, ∀n. In this ex, k = 1)
→ 1 < log2 n(≡ logn) < n < n2 < 2n < n!
• Growth of logn < growth of n → logn is O (n)
• In general, growth of f (n) ≤ growth of g (n)
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Example.
(constant that mean f (n) = k, ∀n. In this ex, k = 1)
→ 1 < log2 n(≡ logn) < n < n2 < 2n < n!
• Growth of logn < growth of n → logn is O (n)
• In general, growth of f (n) ≤ growth of g (n)
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Example.
(constant that mean f (n) = k, ∀n. In this ex, k = 1)
→ 1 < log2 n(≡ logn) < n < n2 < 2n < n!
• Growth of logn < growth of n → logn is O (n)
• In general, growth of f (n) ≤ growth of g (n) → f (n) is
O (g (n))
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Big-O Notation
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Big-O Notation
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Example.
Show that f (x ) = x 2 + 2x + 1 is O (x 2 ).
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Example.
Show that f (x ) = x 2 + 2x + 1 is O (x 2 ).
Solution.
∀x > 1 =⇒ x 2 > 1 ∧ x 2 > x
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Example.
Show that f (x ) = x 2 + 2x + 1 is O (x 2 ).
Solution.
∀x > 1 =⇒ x 2 > 1 ∧ x 2 > x
f (x ) = x 2 + 2x + 1 < x 2 + 2x 2 + x 2 = 4x 2
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Example.
Show that f (x ) = x 2 + 2x + 1 is O (x 2 ).
Solution.
∀x > 1 =⇒ x 2 > 1 ∧ x 2 > x
f (x ) = x 2 + 2x + 1 < x 2 + 2x 2 + x 2 = 4x 2
Let g (x ) = x 2
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Example.
Show that f (x ) = x 2 + 2x + 1 is O (x 2 ).
Solution.
∀x > 1 =⇒ x 2 > 1 ∧ x 2 > x
f (x ) = x 2 + 2x + 1 < x 2 + 2x 2 + x 2 = 4x 2
Let g (x ) = x 2
We have C = 4, k = 1, |f (x )| ≤ C |g (x )|
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Example.
Show that f (x ) = x 2 + 2x + 1 is O (x 2 ).
Solution.
∀x > 1 =⇒ x 2 > 1 ∧ x 2 > x
f (x ) = x 2 + 2x + 1 < x 2 + 2x 2 + x 2 = 4x 2
Let g (x ) = x 2
We have C = 4, k = 1, |f (x )| ≤ C |g (x )|
Thus, f (x ) is O (x 2 )
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
The growth of functions commonly used in big-O
estimates.
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
The growth of functions commonly used in big-O
estimates.
1 < logn < n < nlogn < n2 < 2n < n!
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
The growth of functions commonly used in big-O
estimates.
1 < logn < n < nlogn < n2 < 2n < n!
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
The growth of functions commonly used in big-O
estimates.
1 < logn < n < nlogn < n2 < 2n < n!
How about n3 , 3n and 4n2 ?
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
The growth of functions commonly used in big-O
estimates.
1 < logn < n < nlogn < n2 < 2n < n!
How about n3 , 3n and 4n2 ?
Note. Cf (n) ∼ f (n) , C is constant.
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Big-O theorem
1. Let f (x ) = an x n + an−1 x n−1 + ... + a1 x + a0 where
a1 , a2 , ....an−1 , an are real numbers. Then, f (x ) is O (x n ).
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Big-O theorem
1. Let f (x ) = an x n + an−1 x n−1 + ... + a1 x + a0 where
a1 , a2 , ....an−1 , an are real numbers. Then, f (x ) is O (x n ).
2. f1 (x ) = O (g1 (x )) ∧ f2 (x ) = O (g2 (x ))
=⇒ (f1 + f2 )(x ) = O (max (|g1 (x )|, |g2 (x )|))
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Big-O theorem
1. Let f (x ) = an x n + an−1 x n−1 + ... + a1 x + a0 where
a1 , a2 , ....an−1 , an are real numbers. Then, f (x ) is O (x n ).
2. f1 (x ) = O (g1 (x )) ∧ f2 (x ) = O (g2 (x ))
=⇒ (f1 + f2 )(x ) = O (max (|g1 (x )|, |g2 (x )|))
3. f1 (x ) = O (g1 (x )) ∧ f2 (x ) = O (g2 (x ))
=⇒ (f1 f2 )(x ) = O (g1 g2 (x ))
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Big-O theorem
1. Let f (x ) = an x n + an−1 x n−1 + ... + a1 x + a0 where
a1 , a2 , ....an−1 , an are real numbers. Then, f (x ) is O (x n ).
2. f1 (x ) = O (g1 (x )) ∧ f2 (x ) = O (g2 (x ))
=⇒ (f1 + f2 )(x ) = O (max (|g1 (x )|, |g2 (x )|))
3. f1 (x ) = O (g1 (x )) ∧ f2 (x ) = O (g2 (x ))
=⇒ (f1 f2 )(x ) = O (g1 g2 (x ))
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Big-O theorem
1. Let f (x ) = an x n + an−1 x n−1 + ... + a1 x + a0 where
a1 , a2 , ....an−1 , an are real numbers. Then, f (x ) is O (x n ).
2. f1 (x ) = O (g1 (x )) ∧ f2 (x ) = O (g2 (x ))
=⇒ (f1 + f2 )(x ) = O (max (|g1 (x )|, |g2 (x )|))
3. f1 (x ) = O (g1 (x )) ∧ f2 (x ) = O (g2 (x ))
=⇒ (f1 f2 )(x ) = O (g1 g2 (x ))
COROLLARY. Suppose that f1 (x ) and f2 (x ) are both
O (g (x )). Then (f1 + f2 )(x ) is O (g (x )).
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Example.
1. Estimate big-oh of functions 100n2 + 23nlogn + 2019
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Example.
1. Estimate big-oh of functions 100n2 + 23nlogn + 2019
2. Give a big-O estimate for (2n2 + 17n)(7logn + 15)
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Example.
1. Estimate big-oh of functions 100n2 + 23nlogn + 2019
2. Give a big-O estimate for (2n2 + 17n)(7logn + 15)
3. Give a big-O estimate for
f (n) = 3nlog (n!) + (n2 + 3)logn, where n is a positive
integer.
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Example.
1. Estimate big-oh of functions 100n2 + 23nlogn + 2019
2. Give a big-O estimate for (2n2 + 17n)(7logn + 15)
3. Give a big-O estimate for
f (n) = 3nlog (n!) + (n2 + 3)logn, where n is a positive
integer.
4. Give a big-O estimate for
f (x ) = (x + 1)log (x 2 + 1) + 3x 2 .
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Solution.
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Quizz
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Quizz
Ans: C
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Quizz
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Quizz
Ans: a,b (hàm nào có O(n))(f (x ) = O (g (x )) mean f(x) is
big-oh of g(x))
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Quizz
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Quizz
Ans: a
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Big-Omega and Big-Theta Notation
• ∃C > 0, k : x ≥ k ∧ |f (x )|≥C |g (x )| → f (x ) = Ω(g (x ))
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Big-Omega and Big-Theta Notation
• ∃C > 0, k : x ≥ k ∧ |f (x )|≥C |g (x )| → f (x ) = Ω(g (x ))
• f (x ) = O (g (x )) ∧ f (x ) = Ω(g (x )) → f (x ) = Θ(g (x ))
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Big-Omega and Big-Theta Notation
• ∃C > 0, k : x ≥ k ∧ |f (x )|≥C |g (x )| → f (x ) = Ω(g (x ))
• f (x ) = O (g (x )) ∧ f (x ) = Ω(g (x )) → f (x ) = Θ(g (x ))
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Big-Omega and Big-Theta Notation
• ∃C > 0, k : x ≥ k ∧ |f (x )|≥C |g (x )| → f (x ) = Ω(g (x ))
• f (x ) = O (g (x )) ∧ f (x ) = Ω(g (x )) → f (x ) = Θ(g (x ))
Example. Show that f (x ) = 1 + 2 + ... + n is Θ(n2 )
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Big-Omega and Big-Theta Notation
• ∃C > 0, k : x ≥ k ∧ |f (x )|≥C |g (x )| → f (x ) = Ω(g (x ))
• f (x ) = O (g (x )) ∧ f (x ) = Ω(g (x )) → f (x ) = Θ(g (x ))
Example. Show that f (x ) = 1 + 2 + ... + n is Θ(n2 )
n(n + 1) n2
f (x ) = 1 + 2 + ... + n = =⇒ ≤ f ( x ) ≤ n2
2 2
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Big-Omega and Big-Theta Notation
• ∃C > 0, k : x ≥ k ∧ |f (x )|≥C |g (x )| → f (x ) = Ω(g (x ))
• f (x ) = O (g (x )) ∧ f (x ) = Ω(g (x )) → f (x ) = Θ(g (x ))
Example. Show that f (x ) = 1 + 2 + ... + n is Θ(n2 )
n(n + 1) n2
f (x ) = 1 + 2 + ... + n = =⇒ ≤ f ( x ) ≤ n2
2 2
=⇒ f (x ) = Ω(n2 ) ∧ f (x ) = Θ(n2 ) =⇒ f (x ) = Θ(n2 )
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Big-Omega and Big-Theta Notation
• ∃C > 0, k : x ≥ k ∧ |f (x )|≥C |g (x )| → f (x ) = Ω(g (x ))
• f (x ) = O (g (x )) ∧ f (x ) = Ω(g (x )) → f (x ) = Θ(g (x ))
Example. Show that f (x ) = 1 + 2 + ... + n is Θ(n2 )
n(n + 1) n2
f (x ) = 1 + 2 + ... + n = =⇒ ≤ f ( x ) ≤ n2
2 2
=⇒ f (x ) = Ω(n2 ) ∧ f (x ) = Θ(n2 ) =⇒ f (x ) = Θ(n2 )
Notes.
1. f (x ) is Ω(g (x )) if and only if g (x ) is O (f (x )).
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Big-Omega and Big-Theta Notation
• ∃C > 0, k : x ≥ k ∧ |f (x )|≥C |g (x )| → f (x ) = Ω(g (x ))
• f (x ) = O (g (x )) ∧ f (x ) = Ω(g (x )) → f (x ) = Θ(g (x ))
Example. Show that f (x ) = 1 + 2 + ... + n is Θ(n2 )
n(n + 1) n2
f (x ) = 1 + 2 + ... + n = =⇒ ≤ f ( x ) ≤ n2
2 2
=⇒ f (x ) = Ω(n2 ) ∧ f (x ) = Θ(n2 ) =⇒ f (x ) = Θ(n2 )
Notes.
1. f (x ) is Ω(g (x )) if and only if g (x ) is O (f (x )).
2. If f (x ) = Θ(g (x )) then f(x) is of order g(x) or f(x) and
g(x) are of the same order (cùng độ tăng).
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Big-Omega and Big-Theta Notation
• ∃C > 0, k : x ≥ k ∧ |f (x )|≥C |g (x )| → f (x ) = Ω(g (x ))
• f (x ) = O (g (x )) ∧ f (x ) = Ω(g (x )) → f (x ) = Θ(g (x ))
Example. Show that f (x ) = 1 + 2 + ... + n is Θ(n2 )
n(n + 1) n2
f (x ) = 1 + 2 + ... + n = =⇒ ≤ f ( x ) ≤ n2
2 2
=⇒ f (x ) = Ω(n2 ) ∧ f (x ) = Θ(n2 ) =⇒ f (x ) = Θ(n2 )
Notes.
1. f (x ) is Ω(g (x )) if and only if g (x ) is O (f (x )).
2. If f (x ) = Θ(g (x )) then f(x) is of order g(x) or f(x) and
g(x) are of the same order (cùng độ tăng).
3. Let f (x ) = an x n + an−1 x n−1 + ... + a1 x + a0 where
a1 , a2 , ....an−1 , an are real numbers. Then, f (x ) is Θ(x n ).
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Introduction
• Computational complexity = Time complexity +
space complexity (not be considered).
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Introduction
• Computational complexity = Time complexity +
space complexity (not be considered).
• Time complexity can be expressed in terms of the
number of operations used by the algorithm.
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Introduction
• Computational complexity = Time complexity +
space complexity (not be considered).
• Time complexity can be expressed in terms of the
number of operations used by the algorithm.
1. Worst-case complexity (tells us how many operations
an algorithm requires to guarantee that it will produce
a solution.).
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Introduction
• Computational complexity = Time complexity +
space complexity (not be considered).
• Time complexity can be expressed in terms of the
number of operations used by the algorithm.
1. Worst-case complexity (tells us how many operations
an algorithm requires to guarantee that it will produce
a solution.).
2. Average-case complexity (the average number of
operations used to solve the problem over all possible
inputs of a given size).
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Example.
{8, 4, 11, 3, 10}
Max=8
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Example.
{8, 4, 11, 3, 10}
Max=8
i = 2(8 ≥ 4) → Max=8
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Example.
{8, 4, 11, 3, 10}
Max=8
i = 2(8 ≥ 4) → Max=8
i = 3(8 < 11) → Max=11
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Example.
{8, 4, 11, 3, 10}
Max=8
i = 2(8 ≥ 4) → Max=8
i = 3(8 < 11) → Max=11
i = 4(11 ≥ 3) → Max=11
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Example.
{8, 4, 11, 3, 10}
Max=8
i = 2(8 ≥ 4) → Max=8
i = 3(8 < 11) → Max=11
i = 4(11 ≥ 3) → Max=11
i = 5(11 ≥ 10) → Max=11
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Example.
{8, 4, 11, 3, 10}
Max=8
i = 2(8 ≥ 4) → Max=8
i = 3(8 < 11) → Max=11
i = 4(11 ≥ 3) → Max=11
i = 5(11 ≥ 10) → Max=11
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Example.
{8, 4, 11, 3, 10}
Max=8
i = 2(8 ≥ 4) → Max=8
i = 3(8 < 11) → Max=11
i = 4(11 ≥ 3) → Max=11
i = 5(11 ≥ 10) → Max=11
Thus, Max=11
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Example.
Describe the time complexity (Worst-case) of the
algorithm for finding the largest element in a set.
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Example.
Describe the time complexity (Worst-case) of the
algorithm for finding the largest element in a set.
→ Number of comparisons f (n) = 2(n − 1) + 1.
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Example.
Describe the time complexity (Worst-case) of the
algorithm for finding the largest element in a set.
→ Number of comparisons f (n) = 2(n − 1) + 1.Thus,
f (x ) = Θ (n).
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Example.
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Example.
Ans:f(n)=n+n=2n is O(n)
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Example.
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Example.
Ans: f (n) = n.n = n2 is O (n2 )
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Example.
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Example.
Ans: f (n) = n + n2 is O (n2 )
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Quizz
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Quizz
Ans: b (Number of comparisons f (n) = 2n + 1)
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Quizz
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Quizz
Ans: a (f (n) = n)
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Algorithms
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Algorithms
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
The Growth of Functions
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
The Growth of Functions
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Complexity of Algorithms
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Complexity of Algorithms
Lai Văn Phút Chapter 3. Algorithms
Algorithms The Growth of Functions Complexity of Algorithms Problems
Complexity of Algorithms
Lai Văn Phút Chapter 3. Algorithms