Online Instructions For Chapter 2: Divide-And-Conquer: Algorithms Analysis and Design (CO3031)
Online Instructions For Chapter 2: Divide-And-Conquer: Algorithms Analysis and Design (CO3031)
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
PART 2
Semester 2 – 2019-2020
Chapter 2.
Divide-and-Conquer Strategy
2.2. Quicksort
2.3. Mergesort
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
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.
9
2.4. External sorting
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
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
M = 4 blocks
Number of runs = br/M = 7
Number of merging phases = logM-1br/M = 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
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
16