Sorting 2
Sorting 2
Sorting 2
Module 2, Lecture 6
Why Sort?
Pass 2, 3, , etc.:
three buffer pages used.
INPUT 1
OUTPUT
INPUT 2
Disk
Disk
3
= log2 N + 1
6,2
9,4
8,7
5,6
3,1
3,4
2,6
4,9
7,8
5,6
1,3
4,7
2,3
4,6
1,3
5,6
8,9
Input file
PASS 0
1-page runs
PASS 1
2
2-page runs
PASS 2
2,3
2 N log 2 N + 1
3,4
4,4
6,7
1,2
3,5
6
8,9
4-page runs
PASS 3
1,2
2,3
3,4
8-page runs
4,5
6,6
7,8
9
...
INPUT 2
...
OUTPUT
...
INPUT B-1
Disk
Disk
5
More on Heapsort
Worst-Case:
What is min length of a run?
How does this arise?
Best-Case:
10
B=1,000
1
1
2
3
3
4
5
5
B=5,000
1
1
2
2
2
3
3
4
B=10,000
1
1
1
2
2
3
3
3
11
Double Buffering
OUTPUT
OUTPUT'
Disk
INPUT k
block size
Disk
INPUT k'
12
Sorting Records!
13
Good idea!
Could be a very bad idea!
14
Index
(Directs search)
AAAAAAAAAAAA
AAAAAAAAAAAA
AAAA
AAAA
AAAAAAAA
AAAA
AAAAAAAA
AAAA
AAAA
AAAA
AAAA
AAAAAAAAAAAA
AAAA
AAAA
AAAA
AAAA
AAAAAAAAAAAA
AAAAAAAAAAAA
AAAAAAAAAAAA
AAAA
AAAA
AAAAAAAA
AAAA
AAAAAAAA
AAAA
AAAA
AAAA
AAAA
AAAAAAAAAAAA
AAAA
AAAA
AAAA
AAAAAAAAAAAA
AAAA
AAAAAAAAAAAA
AAAAAAAAAAAA
AAAA
AAAA
AAAAAAAA
AAAA
AAAAAAAA
AAAA
AAAA
AAAA
AAAA
AAAAAAAAAAAA
AAAA
AAAA
AAAA
AAAAAAAAAAAA
AAAA
Data Entries
("Sequence set")
Data Records
15
AAAAAAAA
AAAA
AAAA
AAAA
AAAAAAAA
AAAA
AAAAAAAA
AAAAAAAA
AAAA
AAAA
AAAAAAAAAAAA
AAAA
AAAA
AAAAAAAA
AAAA
AAAA
AAAAAAAA
AAAAAAAA
AAAAAAAA
AAAA
AAAA
AAAA
AAAAAAAA
AAAA
AAAAAAAA
AAAAAAAA
AAAA
AAAAAAAA
AAAAAAAA
AAAA
AAAA
AAAAAAAA
AAAA
AAAA
AAAAAAAA
AAAAAAAA
AAAAAAAA
AAAA
AAAA
AAAA
AAAAAAAA
AAAA
AAAAAAAA
AAAAAAAA
AAAA
AAAAAAAA
AAAAAAAA
AAAA
AAAA
AAAAAAAA
AAAA
AAAA
AAAAAAAA
AAAAAAAA
Data Entries
("Sequence set")
Data Records
Database Management Systems, R. Ramakrishnan
16
Sorting
p=1
p=10
p=100
200
2,000
40,000
600,000
8,000,000
80,000,000
100
1,000
10,000
100,000
1,000,000
10,000,000
1,000
10,000
100,000
1,000,000
10,000,000
100,000,000
10,000
100,000
1,000,000
10,000,000
100,000,000
1,000,000,000
17
Summary
18
Summary, cont.
19