T he Number of Bit Comparisons Used by Quicksort: An Average-case Analysis James Allen Fill Abstract The analyses of many algorithms and data structures (such as digital search trees) for searching and sorting are based on the representation of the keys involved as bit strings and so count the number of bit comparisons. On the other hand, the standard analyses of many other algorithms (such as Quicksort) are performed in terms of the number of key comparisons. We intro duce the prosp ect of a fair comparison between algorithms of the two types by providing an averagecase analysis of the number of bit comparisons required by Quicksort. Counting bit comparisons rather than key comparisons intro duces an extra logarithmic factor to the asymptotic average total. We also provide a new algorithm, "BitsQuick", that reduces this factor to constant order by eliminating needless bit comparisons. Svante Janson Research supp orted by NSF grant DMS-0104167 and by the Acheson J. Duncan Fund for the Advancement of Research in Statistics. Department of Mathematics, Uppsala University, P. O. Box 480, SE-751 06 Uppsala, Sweden. svante.janson@math.uu.se 1 Intro duction and summary Algorithms for sorting and searching (together with their accompanying analyses) generally fall into one of two categories: either the algorithm is regarded as comparing items pairwise irresp ective of their internal structure (and so the analysis focuses on the number of comparisons), or else it is recognized that the items (typically numbers) are represented as bit strings and that the algorithm operates on the individual bits. Typical examples of the two types are Quicksort and digital search trees, respectively; see [7]. In this extended abstract we take a first step towards bridging the gap between the two points of view, in order to facilitate run-time comparisons across the gap, by answering the following question posed many years ago by Bob Sedgewick [personal communication]: What is the bit complexity of Quicksort? E Bn n(ln n)(lg n) as n . More precisely, we consider Quicksort (see Section 2 for a review) applied to n distinct keys (num- Throughout, we use ln (resp ectively, lg) to denote natural (resp., binary) logarithm, and use log when the base Department of Mathematical Sciences, The Johns Hopkins doesn't matter (for example, in remainder estimates). . University, 34th and Charles Streets, Baltimore, MD 21218-2682 The symb ol = is used to denote approximate equality, . USA. jimfill@jhu.edu and = 0.57722 is Euler's constant. bers) from the interval (0, 1). Many authors (Knuth [7], R´gnier [10], R¨sler [11], Knessl and Szpankowski [6], e o Fill and Janson [3] [4], Neininger and Ruschendorff [9], and others) have studied Kn , the (random) number of key comparisons performed by the algorithm. This is a natural measure of the cost (run-time) of the algorithm, if each comparison has the same cost. On the other hand, if comparisons are done by scanning the bit representations of the numbers, comparing their bits one by one, then the cost of comparing two keys is determined by the number of bits compared until a difference is found. We call this number the number of bit comparisons for the key comparison, and let Bn denote the total number of bit comparisons when n keys are sorted by Quicksort. We assume that the keys X1 , . . . , Xn to be sorted are indep endent random variables with a common continuous distribution F over (0, 1). It is well known that the distribution of the number Kn of key comparisons does not depend on F . This invariance clearly fails to extend to the number Bn of bit comparisons, and so we need to specify F . For simplicity, we study mainly the case that F is the uniform distribution, and, throughout, the reader should assume this as the default. But we also give a result valid for a general absolutely continuous distribution F over (0, 1) (sub ject to a mild integrability condition on the density). In this extended abstract we focus on the mean of Bn . One of our main results is the following Theorem 1.1, the concise version of which is the asymptotic equivalence Theorem 1.1. If the keys X1 , . . . , Xn are independent and uniformly distributed on (0, 1), then the number Bn of bit comparisons required to sort these keys using Copyright © 2004 by the Association for Computing Machinery, Inc. and the Society for industrial and Applied Mathematics. All Rights reserved. Printed in The United States of America. No part of this book may be reproduced, stored, or transmitted in any manner without the written permission of the publisher. For information, write to the Association for Computing Machinery, 1515 Broadway, New York, NY 10036 and the Society for Industrial and Applied Mathematics, 3600 University City Science Center, Philadelphia, PA 19104-2688 300 Q uicksort has expectation given by the fol lowing exact and asymptotic expressions: (1.1) (1.2) E Bn = 2 kn (-1)k n 1 =2 k (k - 1)k [1 - 2-(k-1) ] For non-uniform distribution F , we have the same leading term for the asymptotic expansion of E B (), but the second-order term is larger. (Throughout, ln+ denotes the positive part of the natural logarithm function. We denote the uniform distribution by unif .) Theorem 1.2. Let X1 , X2 , . . . be independent with a common distribution F over (0, 1) having density f , and 1 et N be independent and Poisson with mean . l If 0 f (ln+ f )4 < , then the expected number of bit comparisons, cal l it µf (), required to sort the keys X1 , . . . , XN using Quicksort satisfies µf () = µunif () + 2H (f ) ln + o( log ) as , where H (f ) := (in bits) of the density f . 0 = n(ln n)(lg n) - c1 n ln n + c2 n + n n + O(log n), where, with := 2 / ln 2, 1 . (4 - 2 - ln 2) = 3.105, c1 := ln 2 1 . 1 2 c2 := (6 - ln 2)2 - (4 - ln 2) + + 2 ln 2 6 6 = 6.872, and n := k i (-1 - i k )ni k k (-1 - i k ) 1 f lg f 0 is the entropy In applications, it may be unrealistic to assume that a specific density f is known. Nevertheless, even in such cases, Theorem 1.2 may be useful since it provides a Z: k=0 measure of the robustness of the asymptotic estimate in is periodic in lg n with period 1 and amplitude smal ler Theorem 1.1. than 5 × 10-9 . Bob Sedgewick (on June 23, 2003, and among others who have heard us speak on the present material) Small periodic fluctuations as in Theorem 1.1 come has suggested that the number of bit comparisons as a surprise to newcomers to the analysis of algorithms for Quicksort might be reduced substantially by not but in fact are quite common in the analysis of digital comparing bits that have to be equal according to the structures and algorithms; see, for example, Chapter 6 results of earlier steps in the algorithm. In the final in [8]. section, we note that this is indeed the case: for a For our further results, it is technically convenient fixed number n of keys, the average number of bit to assume that the number of keys is no longer fixed comparisons in the improved algorithm (which we dub at n, but rather Poisson distributed with mean and "BitsQuick") is asymptotically 2(1 + 2 l3 2 )n ln n, only n . indep endent of the values of the keys. (In this extended a constant (= 3.2) times the average number of key abstract, we shall not deal with the "de-Poissonization" comparisons [see (2.2)]. A related algorithm is the needed to transfer results back to the fixed-n model; for digital version of Quicksort by Roura [12]; it too requires the results here we can simply compare the fixed-n case (n log n) bit comparisons (we do not know the exact to Poisson cases with slightly smaller and larger means, constant factor). say n ± n2/3 .) In obvious notation, the Poissonized We may compare our results to those obtained version of (1.2) is for radix-based metho ds, for example radix exchange (1.3) sorting, see [7, Section 5.2.2]. This method works E B () = (ln )(lg ) - c1 ln + c2 + + O(log ), by bit insp ections, that is comparisons to constant whose proof is indicated in Section 5 [following (5.2)]. bits, rather than pairwise comparisons. In the case We will also see (Proposition 5.1) that Var B () = of n uniformly distributed keys, radix exchange sorting O(2 ), so B () is concentrated about its mean. Since uses asymptotically n lg n bit insp ections. Since radix the number K () of key comparisons is likewise concen- exchange sorting is designed so that the number of bit trated about its mean E K () 2 ln for large (see insp ections is minimal, it is not surprising that our results show that Quicksort uses more bit comparisons. Lemmas 5.1 and 5.2), it follows that More precisely, Theorem 1.1 shows that Quicksort uses about ln n times as many bit comparisons as radix B ( ) 2 × 1 in probability as . (1.4) exchange sorting. For BitsQuick, this is reduced to lg K () a small constant factor. This gives us a measure of In other words, about 1 lg bits are compared per key the cost in bit comparisons of using these algorithms; 2 comparison. Quicksort is often used because of other advantages, 301 a nd our results opens the possibility to see when they out-weight the increase in bit comparisons. In Section 2 we review Quicksort itself and basic facts about the number Kn of key comparisons. In Section 3 we derive the exact formula (1.1) for E Bn , and in Section 4 we derive the asymptotic expansion (1.2) from an alternative exact formula that is somewhat less elementary than (1.1) but much more transparent for asymptotics. In the transitional Section 5 we establish certain basic facts about the moments of K () and B () in the Poisson case with uniformly distributed keys, and in Section 6 we use martingale arguments to establish Theorem 1.2 for the expected number of bit comparisons for Poisson() draws from a general density f . Finally, in Section 7 we study the improved BitsQuick algorithm discussed in the preceding paragraph. Remark 1.1. The results can be generalized to bases other than 2. For example, base 256 would give corresp onding results on the "byte complexity". Remark 1.2. Cutting off and sorting small subfiles differently would affect the results in Theorems 1.1 and 1.2 by O(n log n) and O( log ) only. In particular, the leading terms would remain the same. Review: numb er of key comparisons used by Quicksort In this section we briefly review certain basic known results concerning the number Kn of key comparisons required by Quicksort for a fixed number n of keys uniformly distributed on (0, 1). (See, for example, [4] and the references therein for further details.) Quicksort, invented by Hoare [5], is the standard sorting procedure in Unix systems, and has been cited [2] as one of the ten algorithms "with the greatest influence on the development and practice of science and engineering in the 20th century." The Quicksort algorithm for sorting an array of n distinct keys is very simple to describ e. If n = 0 or n = 1, there is nothing to do. If n 2, pick a key uniformly at random from the given array and call it the "pivot". Compare the other keys to the pivot to partition the remaining keys into two subarrays. Then recursively invoke Quicksort on each of the two subarrays. With K0 := 0 as initial condition, Kn satisfies the distributional recurrence relation Kn = KUn -1 + Kn-Un + n - 1, L L over the set {1, . . . , n}, Kj = Kj , and U n ; K 0 , . . . , K n- 1 ; K 0 , . . . , K n- 1 L are all indep endent. Passing to expectations we obtain the "divide-andconquer" recurrence relation E Kn = n- 1 2j E K j + n - 1, n =0 which is easily solved to give (2.1) E Kn = 2(n + 1)Hn - 4n (2.2) = 2n ln n + (2 - 4)n + 2 ln n + (2 + 1) + O(1/n). It is also routine to use a recurrence to compute explicitly the exact variance of Kn . In particular, the asymptotics are Var Kn = 2 n2 - 2n ln n + O(n) . where 2 := 7 - 2 2 = 0.4203. Higher moments can be 3 handled similarly. Further, the normalized sequence Kn := (Kn - µn )/n, n 1, 2 3 converges in distribution to K , where the law of K is characterized as the unique distribution over the real line with vanishing mean that satisfies a certain distributional identity; and the moment generating functions of Kn converge to that of K . Exact mean numb er of bit comparisons In this section we establish the exact formula (1.1), repeated here as (3.1) for convenience, for the expected number of bit comparisons required by Quicksort for a fixed number n of keys uniformly distributed on (0, 1): n 1 kn . (-1)k (3.1) E Bn = 2 k (k - 1)k [1 - 2-(k-1) ] =2 Let X1 , . . . , Xn denote the keys, and X(1) < · · · < X(n) their order statistics. Consider ranks 1 i < j n. Formula (3.1) follows readily from the following three facts, all either obvious or very well known: · The event Cij := {keys X(i) and X(j ) are compared} and the random vector (X(i) , X(j ) ) are indep endent. · P(Cij ) = 2/(j - i + 1). [Indeed, Cij equals the event that the first pivot chosen from among X(i) , . . . , X(j ) is either X(i) or X(j ) .] n 1, where = denotes equality in law (i.e., in distribution), and where, on the right, Un is distributed uniformly 302 · The joint density gn,i,j of (X(i) , X(j ) ) is given by gn,i,j (x, y ) = × n i - 1, 1, j - i - 1, 1, n - j xi-1 (y - x)j -i-1 (1 - y )n-j . Let b(x, y ) denote the index of the first bit at which the numbers x, y (0, 1) differ, where for definiteness we take the non-terminating expansion for terminating rationals. Then (3.2) E Bn = = 0 1 P(Cij ) i 0, and asymptotics as 0 are trivial: E K () = 2 0 (-y )[ 1 +O(y )] dy = 1 2 +O(3 ). 2 2 We omit the proof of the result for , but plan to include it in our full-length paper; comparing the fixed-n and Poisson() expansions, note the difference in constant terms and the much smaller error term in the Poisson case. To handle the number of bit comparisons, we will also need the following bounds on the moments of K (). Together with Lemma 5.1, these bounds also establish concentration of K () about its mean when is large. 1/p For real 1 p < , we let W p := (E |W |p ) p denote L -norm and use E(W ; A) as shorthand for the expectation of the product of W and the indicator of the event A. Observe that (5.1) B ( ) = k B k ( ) = k j2 k B k ,j ( ) . =0 =0 =1 A simplification provided by our Poissonization is that, for each fixed k , the variables Bk,j () are indep endent. Further, the marginal distribution of Bk,j () is simply that of K (2-k ). Taking expectations in (5.1), we find (5.2) µunif () = E B () = k 2k E K (2-k ). =0 In the full-length paper we (tentatively) plan to include the details of a proof we have written showing how the two asymptotic estimates in Lemma 5.1 can be used in conjunction with (5.2) to establish the asymptotic estimate (1.3) for µunif () as . [An alternative approach is to Poissonize (1.2).] Moreover, we are now in position to establish the concentration of B () about µunif () promised just prior to (1.4). 304 P roposition 5.1. There exists a constant c such that Var B () c2 2 for 0 < < . Proof. For 0 < < , we have by the triangle inequalL ity for · 2, indep endence and Bk,j () = K (2-k ), and - k /2 Lemma 5.2, with c := c2 k=0 2 , [VarB ()]1/2 k [Var Bk ()]1/2 =0 Lemma 6.1. With f := f , (fk )0k is a martingale, and fk f almost surely (and in L1 ). Before we begin the proof of Theorem 1.2 we remark that the asymptotic inequality µf () µunif () observed there in fact holds for every 0 < < . Indeed, µ f ( ) = (6.1) k j2 =0 k c. k [2k Var K (2-k )]1/2 =0 E K (pk,j ) Remark 5.1. (a) In the full-length paper we will show that Proposition 5.1 can be extended to B() - E B () p cp for any real 1 p < (and some finite cp ) and all 1. =0 =1 k k 2 E K (2-k ) = µunif (), where the first equality appropriately generalizes (5.2), the inequality follows by the convexity of E K () (recall Lemma 5.1), and the second equality follows by (5.2). Proof (sketch) of Theorem 1.2. Assume 1 and, with m m() := lg , split the double sum in (6.1) k as km j2 E K ( pk , j ) + R ( ) , (6.2) µ f ( ) = =0 =1 (b) It is quite plausible that the variables Bk () are positively correlated [because, for k 1, the (k + 1)st bits of two keys can't be compared unless the k th bits are], in which case it is easy to check that Var B () = (2 ) for 1, but we do not know a proof. We would then have B() - E B () p = () for each real 2 p < . Perhaps it is even true that [B () - E B ()]/ has a limiting distribution, but we have no conjecture as to its form. 6 Mean numb er of bit comparisons for keys drawn from an arbitrary density f with R() a remainder term. Assuming f (6.3) (ln+ f )3 < (to be proved in the full-length paper from the assumption on f in the statement of the theorem, using the maximal inequality for nonnegative submartingales) and using the estimates of Lemma 5.1, one can show R() = O(). Plugging this and the consequence E K (x) = 2x ln x + (2 - 4)x + O(x1/2 ), which holds uniformly in 0 x < , of Lemma 5.1 into (6.2), we find k km j2 2 pk,j (ln + ln pk,j ) + (2 - 4)pk,j µ f ( ) = =0 =1 In this section we outline martingale arguments for proving Theorem 1.2 for the expected number of bit comparisons for Poisson() draws from a rather general density f . (For background on martingales, see any standard measure-theoretic probability text, e.g., [1].) In addition to the notation above, we will use the following: I f, pk,j := k,j fk,j := (average value of f over Ik,j ) = 2k pk,j , fk (x) := fk,j for all x Ik,j , f (·) := sup fk (·). k +O = km 2 =0 ln + 2 j Note for each k 0 that pk,j = 1 and that fk : (0, 1) [0, ) is the smoothing of f to the rank-k dyadic rational intervals. From basic martingale theory we have immediately the following simple but key observation. j2 k ( pk,j ) 1 /2 + O ( ) =1 pk,j ln pk,j + (2 - 4) k 1 /2 k /2 +O = µunif () + 2 km f 2 + O ( ) ln fk + O(), =0 305 w here we have used the Cauchy­Schwarz inequality at the second equality and comparison with the uniform case (f 1) at the third. But, by Lemma 6.1, (6.3), and the dominated convergence theorem, f f (6.4) ln f as k , k ln fk - from which follows µf () = µunif () + 2(lg ) = µunif () + 2(ln ) as desired. Remark 6.1. If we make the stronger assumption that f is H¨lder() continuous on [0, 1] for some > 0, o then we can quantify (6.4) and improve the o( log ) remainder in the statement of Theorem 1.2 to O(). 7 An improvement: BitsQuick Recall the operation of Quicksort describ ed in Section 2. Suppose that the pivot [ call it x = 0.x(1) x(2) . . . ] has first bit x(1) equal to 0, say. Then the subarray of keys smaller than x all have first bit equal to 0 as well, and it wastes time to compare first bits when Quicksort is called recursively on this subarray. We call BitsQuick the obvious recursive algorithm that does away with this waste. We give one possible implementation in the boxed pseudo code, where L(y ) denotes the result of rotating the register containing key y to the left --i.e., replacing y = .y (1) y (2) . . . y (m) by .y (2) . . . y (m) y (1) [and similarly R(A) for rotation of every element of array A to the right]. The input bit b indicates whether or not the array elements need to be rotated to the right before the routine terminates. The symbol denotes concatenation (of sorted arrays). (We omit minor implementational details, such as how to do sorting in place and to maintain random ordering for the generated subarrays, that are the same as for Quicksort and very well known.) The routine BitsQuick(A, b) receives a (randomly ordered) array A and a single bit b, and returns the sorted version of A. The initial call is to BitsQuick(A0 , 0), where A0 is the full array to be sorted. A related but somewhat more complicated algorithm has been considered by Roura [12, Section 5]. In the full-length paper we plan to present arguments justifying the number of bit comparisons used by f ln f + o( log ) lg f + o( log ), The routine BitsQuick(A, b) If |A| 1 Return A Else Set A- and A+ Cho ose a random pivot key x = 0.x(1) x(2) . . . If x(1) = 0 For y A with y = x If y < x Set y L(y ) and then A- A- {y } Else Set A+ A+ {y } Set A- BitsQuick(A- , 1) and A+ BitsQuick(A+ , 0) Set A A- {x} A+ Else For y A with y = x If y < x Set A- A- {y } Else Set y L(y ) and then A+ A+ {y } Set A- BitsQuick(A- , 0) and A+ BitsQuick(A+ , 1) Set A A- {x} A+ If b = 0 Return A Else Return R(A) BitsQuick as a fair measure of its overall cost. In the full-length paper we will also prove the following analogue of Theorem 1.1 (and establish further terms in the asymptotic expansion), wherein 3 15 7 . + - + 2 = 13.9 c1 := ~ ln 2 2 ln 2 and n := ~ 1k ln 2 3 - i k (-1 - i k ) ni k 1 + i k f Z: k=0 is periodic in lg n with period 1 and amplitude smaller than 2 × 10-7 : nk 2+ kn (k - 2) k-4 -1 (-1)k E Qn = - -k k 1-2 1 - 2-(k-1) =2 = We have not yet had the opportunity to consider the variability of Qn . 2 + 3 ln 2 n 2nHn - 5n + 2Hn + 1 ln n - c1 n + n n + O(log2 n). ~ ~ 306 R eferences [1] Kai Lai Chung. A course in probability theory. Second edition, Probability and Mathematical Statistics, Vol. 21, Academic Press, New York­London, 1974. [2] J. Dongarra and F. Sullivan. Guest editors' intro duction: the top 10 algorithms. Computing in Science & Engineering, 2(1):22­23, 2000. [3] James Allen Fill and Svante Janson. Smo othness and decay prop erties of the limiting Quicksort density function. Mathematics and Computer Science (Versail les, 2000), pp. 53­64. Trends Math., Birkh¨user, Basel, a 2000. [4] James Allen Fill and Svante Janson. Quicksort asymptotics. J. Algorithms, 44(1):4­28, 2002. [5] C. A. R. Hoare. Quicksort. Comput. J., 5:10­15, 1962. [6] Charles Knessl and Wo jciech Szpankowski. Quicksort algorithm again revisited. Discrete Math. Theor. Comput. Sci., 3(2):43­63 (electronic), 1999. [7] Donald E. Knuth. The Art of Computer Programming. Vol. 3: Sorting and Searching. 2nd ed., AddisonWesley, Reading, Mass., 1998. [8] Hosam M. Mahmoud. Evolution of random search trees. John Wiley & Sons Inc., New York, 1992. [9] Ralph Neininger and Ludger Ruschendorf. Rates of ¨ convergence for Quicksort. J. Algorithms 44(1):51­62, 2002. [10] Mireille R´gnier. A limiting distribution for quicksort. e RAIRO Inform. Th´or. Appl., 23(3):335­343, 1989. e [11] Uwe R¨sler. A limit theorem for "Quicksort". RAIRO o Inform. Th´or. Appl., 25(1):85­100, 1991. e [12] Salvador Roura. Digital access to comparison-based tree data structures and algorithms. J. Algorithms, 40(1):1­23, 2001. 307