

 
list-type mergesort (list-type L; int n)
{
if (n = 1)
return (L)
else {
split L into two halves L1and L2;
return (merge (mergesort  L1,
L1,
 ,
mergesort
,
mergesort 
 L2,
L2,

 
}
}
 
Let T(n) be the running time of mergesort on a list of size n. Then.
| T(n) |  | C1 (n = 1) | |
|  |  +  (n > 1) | 
It can be shown that T(n) is O(nlog n).
Mergesort as an External Sorting Procedure
 
 
 
A run is a sequence of records r1,...,
rk,
such that ri ri
+ 1 for i = 1,...,
k
- 1.
ri
+ 1 for i = 1,...,
k
- 1.
 i
i  1 such that ki
1 such that ki  m,
the sequence
m,
the sequence  rk(i
- 1) + 1, rk(i - 1) + 2,..., rki
rk(i
- 1) + 1, rk(i - 1) + 2,..., rki
The following is organized into runs of length 3 with the tail having length 2.
| 7 15 29 | 8 11 13 | 16 22 31 | 5 12 | 
Consider two files f1 and f2 organized into runs of length k. Assume that
 1
1Algorithm
Example
| f1 | 28 | 3 | 93 | 54 | 65 | 30 | 90 | 10 | 69 | 8 | 22 | 
| f2 | 31 | 5 | 96 | 85 | 9 | 39 | 13 | 8 | 77 | 10 | 
 merge into
g1 and g2
merge into
g1 and g2
| g1 | 28 | 31 | 93 | 96 | 9 | 65 | 13 | 90 | 69 | 77 | 22 | 
| g2 | 3 | 5 | 54 | 85 | 30 | 39 | 8 | 10 | 8 | 10 | 
 merge into
f1 and f2
merge into
f1 and f2
| f1 | 3 | 5 | 28 | 31 | 9 | 30 | 39 | 65 | 8 | 10 | 69 | 77 | 
| f2 | 54 | 85 | 93 | 96 | 8 | 10 | 13 | 90 | 22 | 
 merge into
g1 and g2
merge into
g1 and g2
| g1 | 3 | 5 | 28 | 31 | 54 | 85 | 93 | 96 | 8 | 10 | 22 | 69 | 77 | 
| g2 | 8 | 9 | 10 | 13 | 30 | 39 | 65 | 90 | 
 merge
into
f1 and f2
merge
into
f1 and f2
| f1 | 3 | 5 | 8 | 9 | 10 | 13 | 28 | 30 | 31 | 
| 39 | 54 | 65 | 85 | 90 | 93 | 96 | |||
| f2 | 8 | 10 | 22 | 69 | 77 | 
 merge into
g1 and g2
merge into
g1 and g2
g1 will be left with the sorted sequence
 n,
then one of the two files will be empty and the other will contain a single
run of length n (which is the sorted sequence).
n,
then one of the two files will be empty and the other will contain a single
run of length n (which is the sorted sequence). 
 
 
Therefore number of passes =  logn
logn 

 .
. 
 
 
Total number of blocks read or written in a pass 
 
(where b is the number of records that fit into a block)
Total number of block reads or writes for the entire sorting}  O
O

 .
.
 
 
 
We can read m runs, one from each file, and merge them into one run of length mk. This run is placed on one of m files g1,..., gm, each getting a run in turn.
 
 
 
By using a priority queue that supports insert and delete in logarithmic time (e.g., partially ordered tree), the process can be accomplished in O(log m)time.
 logmn
logmn
 O(nlog2m.logmn)
O(nlog2m.logmn)




