Beruflich Dokumente
Kultur Dokumente
net/publication/225952172
CITATIONS READS
15 782
2 authors:
Some of the authors of this publication are also working on these related projects:
All content following this page was uploaded by Wolfgang Panny on 05 June 2014.
1
Department of Computer Science, University of Economics,
Augasse 2{6, A-1090 Vienna, Austria.
E-mail: wolfgang.panny@wu-wien.ac.at
2
Department of Algebra and Discrete Mathematics, Technical Uni-
versity, Wiedner Hauptstrasse 8{10, A-1040 Vienna, Austria.
E-mail: proding@email.tuwien.ac.at
1. Introduction
Mergesort is one of the most prominent internal sorting methods. Especially for
lists it is one of the best available alternatives (cf. [Knuth], [Gonnet]). Among the
particular advantages of mergesort | also compared to Quicksort and its variants
| it maintains its O(N log N ) time complexity also in the worst case (in fact the
worst case time is only O(N ) worse than the average time) and it is stable.
There are two basic variants of mergesort, namely top-down mergesort and bottom-
up mergesort. Yet another variant | termed queue mergesort | recently has been
proposed in [Golin, Sedgewick]. For both of the basic variants the behavior of the
quantities determining the running time of the pertaining algorithms is basically
known (cf. e.g. [Gonnet, p.173], [Knuth, ex.5.2.4-13]). Of these quantities the
number C (N ) of comparisons necessary to sort N elements is perhaps the most
important since it has the strongest impact on running time.
One interesting feature about mergesort is the occurrence of periodicity phenom-
ena | which of course are not uncommon in the analysis of divide-and-conquer
algorithms. Let us introduce the notation Cb;td(N ), Cw;td(N ) and Ca;td(N ) for the
number of comparisons of the top-down algorithm in the best, worst and average
case, respectively. In a recent paper Flajolet and Golin have shown how to deal with
y Accepted for publication in Algorithmica (April 5, 1994)
1
Mergesort, File: msfinal6.tex Date: 25. 1. 1995(678)
such periodicity phenomena encountered in the analysis of Cw;td(N ) and Ca;td(N ).
One has for example
Cw;td(N ) = N lg N + NA(lg N ) + 1; (1)
where the periodic function A is dened by A(x) = 1 ? fxg ? 2 ?fxg. It should
1
be evident that this last expression describes the behavior of Cw;td(N ) better than
the conventional expression dlg N eN ? 2d N e + 1 since it explicitely re
ects the
lg
2. Bottom-Up Mergesort
The basic scheme of bottom-up mergesort may be characterized in the following
way:
Algorithm MergeSort(a[1::N ])
begin
length := 1;
while length <N do
begin
p := 0; q := length ;
while q + length N do
begin
Merge (a[p + 1::p + length ], a[q + 1::q + length ]);
p := q + length ; q := p + length
end;
if q < N then Merge (a[p + 1::p + length ], a[q + 1::N ]);
length := 2 length
end
end;
The following recursive formulation shows that bottom-up mergesort | like the
top-down variant | follows the divide-and-conquer paradigm. Only the splitting
strategy is dierent. It is just this \power of two" splitting strategy | as opposed
to the \half and half" strategy of the top-down variant | which makes it possible
to devise the above nonrecursive algorithm.
2
Mergesort, File: msfinal6.tex Date: 25. 1. 1995(678)
Algorithm MergeSort (a[1 :: N ])
begin
if N 2
then begin
MergeSort dlg N e?1 ;
(a[1 :: 2 ])
MergeSort dlg N e?1
(a[2 +1 :: N ])
;
Merge dlg N e?1
(a[1 :: 2
, dlg N e?1
] a[2 +1 :: N ])
end
end;
N mod 4 > 2 there is also an \incomplete" merge process of type (2; N mod 2). In
general, at level j (j 0) there are bN=2j c complete merge processes of type
+1
type (2j ; N mod 2j ). Whether such an incomplete merge process occurs at level
j is easily seen from the binary representation of N : it takes place if and only if
bj (bj? bj? : : : b b ) > 0.
1 2 1 0 2
The cost measures Cb;bu(N ), Cw;bu(N ) and Ca;bu(N ) are, of course, ultimately
based on the quantities cn;m, denoting the number of comparisons in the single
(n; m) merge processes. It is well known (cf. e.g. [Knuth, ex.5.2.4-2]) that
minfn; mg cn;m n + m ? 1 (2)
and that ? j? ? j?
+ 1 1
Pfcn;m = j g = n? ?n mm? ; 1
+
1
(3)
n
under the usual assumptions. The factorial moments are (cf. [Panny])
Efcrn;mg = nm n +1 r + m 1+ r [n + m + 1]r? ;
(4) 1
where [x]k = x(x + 1) : : : (x + k ? 1). The variables cn;m related to the single merge
processes are independent of each other.
2.1 The best Case
The overall best case Cb;bu(N ) is realized exactly if every single merge process is
best. Hence, according to (2), one only has to sum the smaller string lengths over
all merge processes:
2j? 2Nj +
X j k X X
Cb;bu(N ) = 1
bj (bj? : : : b b ) =1 (n); (5)
1 0 2
<j k
0 <j k 0 n<N
3
Mergesort, File: msfinal6.tex Date: 25. 1. 1995(678)
where (n) denotes the number of 1's in the binary representation of n. The
right hand side equality is easily seen by the following inductive argument: Let
Cb;bu(N + 1) = Cb;bu(N ) + (N ). The bottom level contributes to (N ) with
one additional comparison if N is odd. At the next level we have one additional
comparison if N mod 4 2. In general, level j contributes to (N ) if and only if
bj = 1. Hence (N ) = (N ). The periodicity and fractal nature of Cb;bu(N ) is
illustrated by Fig.1.
-0.05
-0.1
-0.15
-0.2
Fig.1: The
uctuation of the low order terms for the best case, expressed by
(Cb;bu(N ) ? (N lg N )=2)=N . Values are plotted against lg N for N = 2 ::2 . 6 9
P
The behavior of n<N (n) has been extensively studied by [Delange]. The so-
called Trollope-Delange formula tells us that
(n) = 21 N lg N + N F (lg N ):
X
0
n<N
By appropriately rewriting (5) one sees that F (x) = F (fxg) and for x 2 [0; 1) this
0 0
j 0
palatable way as
F (x) = 21x j ? 12 (0: 0| :{z: : 0} j j : : :) ? x2 :
X
0 +1 +2 2
j 0 j 0's
with an explicit Fourier expansion involving Riemann's zeta function (cf. [Delange],
[Flajolet et al.]). The period of F is 1 and its Fourier series
0
X
F (x) = 0 fk e ikx 2
k ; ;:::
=0 1
4
Mergesort, File: msfinal6.tex Date: 25. 1. 1995(678)
has the coecients
f = lg2 ? 2 log
0
1 ? 1 = ?0:145599455 : : :;
2 4
fk = ? log1 2 ((k+) 1) for k 6= 0;
k k
where
ik :
k = 2log 2
The coecient f tells us that F (x) on the average has a value of ?0:145599455 : : : .
The maximum of 0 is attained at the integer points. The minimum of (lg 3)=2 ? 1 =
0 0
<j k 0 <j k 0
By properly rewriting (6) one gets the following representation for Cw;bu(N )
h X i
N lg N + N (bj ? 1)(bj? bj? : : : b ) =N ? flg N g ? 2v2 N =N + 1 ;
1 2 0 2
( )
<j k
0
which shows that the main term is N lg N and that the bracketed expression remains
constant, when N is replaced by 2N . Fig.2 illustrates the periodical
uctuation of
the linear term.
The last representation suggests the introduction of the function F (x), dened by 1
fxg j
F (x) = F (fxg) = 2f1xg 2fxg j ? 2 2fxg j? ? 1 f2 2j g ? fxg:
X +
+ + 1
1 1
j 0
1
F (x) = 2fxg
X
j ? 1 (0: 0| :{z: : 0} j j : : :) ? fxg:
1 +1 +2 2
j 0 j 0's
5
Mergesort, File: msfinal6.tex Date: 25. 1. 1995(678)
-0.2
-0.4
-0.6
-0.8
-1
Fig.2: The
uctuation of the low order terms for the worst case, expressed by
(Cw;bu(N ) ? N lg N )=N . Values are plotted against lg N for N = 2 :: 2 . 6 9
For the remaining term 2v2 N =N we may introduce the function F (x), dened by
( )
2
?m?fxg ;
F (x) = F (fxg) = 2 if 2fxg = (1: : : : m ) is dyadic;
1 2 2
0; elsewhere.
2 2
By means of the functions F (x) and F (x) the linear term of Cw;bu(N ) can be
1 2
expressed by
N (F (lg N ) ? F (lg N )):
1 2
In the course of his study of binomial queues Cheno has encountered the function
j 0
which is closely related to the functions F (x) and F (x). Obviously, F (x) may be
0 1 1
expressed by
F (x) = F (x) + 21 G(x) ? 21 :
1 0
G(x) is periodic with period 1 and has an explicit Fourier series with coecients
(cf. [Cheno])
g = 21 +
log?21 = ?0:1099488636 : : :;
0
gk = log1 2 (1 + k ) for k 6= 0;
1 + k
where again
ik :
k = 2log 2
6
Mergesort, File: msfinal6.tex Date: 25. 1. 1995(678)
Since G is not continuous we can not assure the convergence of its Fourier series,
hence | apart from g | the coecients do not make too much sense. But they
0
k ; ;:::
=0 1
viz.
?
? 1 = ?0:7005738875 : : :;
c = lg2 ? 22log
0
2 2
1 ( + 1) ( )
k k
ck = ( + 1) log 2
k 2 ? for k 6= 0:
k
By examining more closely the F -function one sees that it is continuous except
at points , where 2fg = (1: : : : m ) is dyadic. In these points F is on-
1
0 1 1 2
But F is zero and continuous for all remaining points. Hence, the only dierence
between F (x) and F (x) ? F (x) is that the latter function is continuous from
2
1 1 2
the left, whereas F is continuous from the right. Consequently, also the Fourier
1
coecients coincide, which entails in particular that the mean value of the linear
term of Cw;bu(N ) is c = ?0:7005738875 : : : .
0
The biggest jumps of A (x) = F (x) ? F (x) occur at integer points. Let denote
such a point. Then limx!? A (x) = A () = ?1, whereas limx! A (x) = 0.
1 2
0 +0
Regarding the
uctuation about the main term this means that the worst case is
\best" if N is a power of 2 and is \worst" if N has the form N = 2m + 1.
Ca;bu(N ) = j? j (7)
<j k 2 + 1 2
1
0
<j k
0
1 2 0 2
=
X
2j 2Nj ? N
j k X 1 + X 2j n N o
2j? + 1
<j k
0
2j? + 1 2j 0 <j k
1
<j k
0
1
7
Mergesort, File: msfinal6.tex Date: 25. 1. 1995(678)
2j N
X j k
0 ?k
= 2 j ? N ( + O(2 )) + O(log N )
<j k
0
X jN k
= 2 2j ? 0 N + O(log N );
j (7a)
<j k
0
P
where the constant 0 is well known, 0 = j (2j + 1)? = 1:26449 97803 : : : , 1
X bj 2j (b b : : : b )
j j? j?
<j k 2 + 1
1 2 0 2
can be expressed as
X bj 2j 2j n N o = X
bj 2j 2Nj + O(log N );
n o
(7b)
j 2j
0<j k 2 + 1 0 <j k
where again the error term does not aect the linear term. Combining (7a) and
(7b) we thus obtain
X jN k X nN o
2j
2j ? 0 N + 2j + O(log N ) bj 2j
0<j k <j k 0
n N o
=
X N
2 2j ? 2j ? 0 N +
j X
bj 2j 2Nj + O(log N )
n o
<j k
0 <j k 0
1
=N lg N + N flg N g
X (b j ? : : : b ) 0
(bj ? 1) blg N c ? flg N g ? + O(log N ) 1 0 2
2 <j k 0
2
=N lg N + N F (lg N ) ? 0 + O(log N ) :
1
0 <j k j? j? 1 2 0 2
-0.4
-0.6
-0.8
-1
-1.2
Fig.3: The lower graph represents the true linear term for the average case, ex-
pressed by (Ca;bu(N ) ? N lg N )=N . The values are plotted against lg N for
N = 2 :: 2 . The upper graph corresponds to the approximating function
8 11
B (x).
where B (x) = A (x) + 1 ? 0 . Hence the mean value of B (x) is c + 1 ?
0 = ?0:96507 36678 : : : . The minimum, realized at the integer points, is ?0 =
0
period 1. F is explicitly dened in section 2.1, where also its Fourier expansion is
given. The mean value of F is ?0:145599455 : : :, its maximum is 0 and its minimum
0
9
Mergesort, File: msfinal6.tex Date: 25. 1. 1995(678)
comparisons in the worst case (cf. e.g. [Gonnet]). Taking the dierence one gets
X
Cw;bu(N ) ? Cw;td(N ) = 2k ? N ? +1
(1 ? bj )(bj? bj? : : : b ) ? 2v2 N :
1 2 0 2
( )
0<j k
But ?
2k ? N = (1 ? bk )(1 ? bk? ) : : : (1 ? b )(1 ? b ) + 1;
+1
1 1 0
2
j k
0 <j k
0
to r we nally obtain
X
Cw;bu(N ) ? Cw;td(N ) = (1 ? bj )[2j ? (bj? bj? : : : b ) ] 0:
1 2 0 2
r<j k
Hence the worst-case number of comparisons for both merging strategies is equal
only for N 's with a binary representation of the form N = (1 0 ) . In general the +
2
bottom-up algorithm performs more comparisons in the worst case. The maximum
for N 2 [2k ; 2k ] occurs for N = 2k +1 sorting elements and amounts to 2k ? k +1
+1
additional comparisons.
Incidentally, the more favorable behavior of the top-down variant in the worst case
is not so surprising because of the close connection between the top-down splitting
strategy and Fano coding, from which we may conclude that the top-down algorithm
has the best worst-case behavior (is minimax-optimal, cf. [Knuth, p.193]) among
all possible splitting strategies.
-0.92
-0.94
-0.96
-0.98
Fig.4: The
uctuation of the low order terms for the worst case, expressed by
(Cw;td(N ) ? N lg N )=N . The values are plotted against lg N for N =
2 :: 2 .
6 9
10
Mergesort, File: msfinal6.tex Date: 25. 1. 1995(678)
Fig.4 shows the
uctuation of the low order terms for the top-down variant. As al-
ready mentioned in the introduction, the worst case behavior of top-down mergesort
can be described by (cf. [Flajolet, Golin])
Cw;td(N ) = N lg N + NA(lg N ) + 1;
where
A(x) = A(fxg) = 1 ? fxg ? 2 ?fxg :
1
The mean value of A(x) is ?0:9426950408 : : : . The extreme values are ?1 and
?(1 + log log 2)= log 2 = ?0:91392 : : :, where the minimum is attained at integer
points and the maximum at the points congruent to 1 + log log 2= log 2.
On the other hand it has been shown in section 2.2 that
Cw;bu(N ) = N lg N + NA (lg N ) + 1;
where A is periodic with period 1. A is continuous from the left with jumps
of 2?m?fg in the points for which 2fg = (1: : : : m ) is dyadic. For the
remaining points A is continuous. A is nowhere dierentiable. The mean value
1 2
-0.2
-0.4
-0.6
-0.8
-1
11
Mergesort, File: msfinal6.tex Date: 25. 1. 1995(678)
-1.22
-1.23
-1.24
-1.25
Fig.5: The
uctuation of the low order terms for the average case, expressed by
(Ca;td(N )?N lg N )=N . The values are plotted against lg N for N = 2 :: 2 . 6 9
+2
X (1?? bj )(2 j ? ?
(bj? : : : b b ) ) :
1 1 0 2
(9)
N
j +1 + 1
N
j +1 + 2
j k
0 2 2
Flajolet and Golin recently have studied the periodical
uctuation of Ca;td(N ) as
shown in Fig.5. They have shown that
Ca;td(N ) = N lg N + NB(lg N ) + O(N ); > 0;
where B(x) is a continuous non-dierentiableP periodic function with period 1 that
has an explicit Fourier expansion B(x) = k ; ; ;::: bk expf2ikxg. The mean
value b of B(x) equals
=0 1 2
1? 1 ? 1 X 2 2j + 1
2 log 2 log 2 j (j + 1)(j + 2)
1
log 2j = ?1:24815204209965388489 : : : :
The remaining Fourier coecients are
bk = log1 2 1 +( (+k1)) ik
where again k = 2log 2
k k
and
X 2 ? 1 1
(s) = (j + 1)(j + 2) (2j )s + (2j + 1)s :
j 0
12
Mergesort, File: msfinal6.tex Date: 25. 1. 1995(678)
-0.6
-0.8
-1
-1.2
at integer points. The maximum is ?1:24075 0572 10? and is attained in the
0
9
4. Concluding Remarks
Our results together with the results of Flajolet and Golin show that the average-
case number of comparisons for both variants dier at most by N . The mean
dierence is only about 0:28N .
Tab.1: Comparison of top-down and bottom-up mergesort. For the linear term
the mean value is shown.
This slightly better performance of the top-down algorithm is contrasted by an
important restriction, namely that the number of elements to be sorted must be
known in advance. This seems to be a serious shortcoming for a sorting algorithm
whose natural application eld is to operate on lists. In the literature it is recom-
mended to overestimate the number of elements in the list, if it is unknown. It
13
Mergesort, File: msfinal6.tex Date: 25. 1. 1995(678)
seems very likely that the narrow time-advantage of the top-down variant gets lost
in that case.
5. References
Cheno, L. (1981) Prols limites d'histoires sur les dictionaires et les ls de priorite;
Application aux les binomiales, These, Universite de Paris-Sud.
Delange, H. (1975) Sur la fonction sommatoire de la fonction \Somme des Chires".
Enseignement Math.(2), 21, 31{47.
Flajolet, P., Grabner, P., Kirschenhofer, P., Prodinger, P., Tichy, R., F. (1994)
Mellin transforms and asymptotics: digital sums. Theoretical Computer Sci-
ence, 123, 291{314.
Flajolet, P., Golin, M. (1992) Mellin transforms and asymptotics: the mergesort
recurrence. INRIA research report, 1612. Accepted for publication in Acta
Informatica.
Foster, D. M. (1987) Estimates for a remainder term associated with the sum of
digits function. Glasgow Math. J., 29, 109{129.
Golin, M., Sedgewick, R. (1993) Queue-mergesort. Inf. Process. Lett., 48, 253{259.
Gonnet, G. H., Baeza-Yates, R. (1991) Handbook of Algorithms and Data Struc-
tures, 2nd ed., Addison-Wesley, Reading, MA.
Knuth, D. E. (1973) The Art of Computer Programming: Sorting and Searching,
Addison-Wesley, Reading, MA.
Panny, W. (1988) Straight two-way mergesort: der Algorithmus und seine Analyse,
in Statistik, Informatik und Okonomie | Josef Roppert zum 60. Geburtstag,
(ed.: W.H. Janko), 216{236, Springer, Berlin.
14