Ho Chi Minh City University of Technology
Faculty of Computer Science and Engineering
Online Instructions for
Chapter 2: Divide-and-conquer
Algorithms Analysis and Design
(CO3031)
Instructor: Dr. Vo Thi Ngoc Chau
Email: chauvtn@hcmut.edu.vn
Semester 2 – 2019-2020
Course Outline
Chapter 1. Fundamentals
Chapter 2. Divide-and-Conquer Strategy
Chapter 3. Decrease-and-Conquer Strategy
Chapter 4. Transform-and-Conquer Strategy
Chapter 5. Dynamic Programming and Greedy
Strategies
Chapter 6. Backtracking and Branch-and-Bound
Chapter 7. NP-completeness
Chapter 8. Approximation Algorithms
2
Ho Chi Minh City University of Technology
Faculty of Computer Science and Engineering
Online Instructions for
Chapter 2: Divide-and-conquer
PART 2
Semester 2 – 2019-2020
Chapter 2.
Divide-and-Conquer Strategy
2.1. Divide-and-conquer strategy
2.2. Quicksort
2.3. Mergesort
2.4. External sorting
2.5. Binary search tree
4
2.3. Mergesort
To sort a given array of n elements, divide it in half,
sort the two halves (recursively), and then merge the
two halves together.
Merging: given two sorted arrays a[1..M] and b[1..N],
we wish to merge them into the third sorted array
c[1..M+N].
i:= 1; j :=1;
for k:= 1 to M+N do
if a[i] < b[j] then
begin c [k] := a[i]; i:= i+1 end
else begin c[k] := b[j]; j := j+1 end;
5
2.3. Mergesort
procedure mergesort(left,right: integer);
var i, j, k, m : integer;
begin
if right-left>0 then
begin
m:=(right+left)/2; mergesort(left,m); mergesort(m+1,right);
for i := m downto left do b[i] := a[i];
for j :=m+1 to right do b[right+m+1-j] := a[j];
//i := left; j := right;
for k :=left to right do Merging on b:
if b[i] < b[j] then
0 6 7 11 9 4
begin a[k] := b[i] ; i := i+1 end i j
else begin a[k] := b[j]; j:= j-1 end;
end;
end; 6
2.3. Mergesort
7
2.3. Mergesort
Time complexity for sorting n elements
Abstract operation: comparison
All the cases:
C1 = 0
Cn = 2Cn/2 + n when n≥2
Cn = nlog2n = O(nlog2n)
NOTE: Mergesort needs memory space of n elements for merging!
8
2.4. External sorting
Sorting the large files stored in secondary storage is
called external sorting. External sorting is very
important in database management systems.
When estimating the computational time of the
algorithms that work on files in hard disks, we must
consider the number of times we read a block to main
memory or write a block to secondary storage.
Such operation is called block access or disk access.
9
2.4. External sorting
The most commonly used technique for external
sorting is the external sort-merge algorithm.
This external sorting method has two stages.
create runs: br = number of blocks, M = buffer size
br/M runs from any in-place in-memory sorting algorithm
merge runs: one block for output, each block of the
rest for each input run
log(M-1)br/M merging phases
10
2.4. External sorting
create runs: br/M runs from in-place in-memory sorting
i = 0;
repeat
read M blocks of the file, or the rest of the file, whichever is
smaller;
sort the in-memory part of the file;
write the sorted data to the run file Ri;
i := i+1;
until the end of the file;
merge runs: one block for output, each block of the rest
for each input run with log(M-1)br/M merging phases
11
2.4. External sorting
create runs: br/M runs from in-place in-memory sorting
merge runs: one block for output, each block of the rest
for each input run with log(M-1)br/M merging phases
read one block of each of the N files Ri into the buffer; //N = br/M
repeat
choose the first tuple (in sort order) among all buffer pages;
write the tuple to the output and delete it from the buffer page;
if the buffer page of any run Ri is empty and not end-of-file(Ri)
then read the next block of Ri into the buffer page;
until all buffer pages are empty;
12
2.4. External sorting
File: 2, 3, 9, 1, 5, 6, 0, 9, 10, 16, 8, 11, 2,
15, 9, 4, 2, 0, 7, 4, 5, 3, 12, 13, 1, 0
1 record/block, 26 records => br=26 blocks
M = 4 blocks
Number of runs = br/M = 7
Number of merging phases=logM-1br/M=2
2,3,9,1, 5,6,0,9, 10,16,8,11, 2,15,9,4, 2,0,7,4, 5,3,12,13, 1,0
Generate runs
1,2,3,9, 0,5,6,9, 8,10,11,16, 2,4,9,15, 0,2,4,7, 3,5,12,13, 0,1
13
2.4. External sorting
File: 2, 3, 9, 1, 5, 6, 0, 9, 10, 16, 8, 11, 2, 15, 9, 4, 2, 0, 7, 4,
5, 3, 12, 13, 1, 0
1 record/block, 26 records => br=26 blocks
M = 4 blocks
Number of runs = br/M = 7
Number of merging phases = logM-1br/M = 2
1,2,3,9, 0,5,6,9, 8,10,11,16, 2,4,9,15, 0,2,4,7, 3,5,12,13, 0,1
Merge in phase 1
0,1,2,3,5,6,8,9,9,10,11,16, 0,2,2,3,4,4,5,7,9,12,13,15, 0,1
Merge in phase 2
0,0,0,1,1,2,2,2,3,3,4,4,5,5,6,7,8,9,9,9,10,11,12,13,15,16
14
2.4. External sorting
Time complexity
Abstract operation: block access
All the cases:
2br + 2br log M-1 br/M = 2br(log M-1 br/M +1)
br = number of blocks,
run generation run merging M = buffer size
15
Algorithms Analysis and Design
(CO3031)
Chapter 2. Divide-and-conquer
2.1. Divide-and-conquer strategy
2.2. Quicksort
2.3. Mergesort
2.4. External sorting
2.5. Binary search tree
16