2.3 Quicksort
Total Page:16
File Type:pdf, Size:1020Kb
Algorithms ROBERT SEDGEWICK | KEVIN WAYNE 2.3 QUICKSORT ‣ quicksort ‣ selection Algorithms ‣ duplicate keys FOURTH EDITION ‣ system sorts ROBERT SEDGEWICK | KEVIN WAYNE https://algs4.cs.princeton.edu Last updated on 3/24/20 5:34 PM Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational infrastructure. ・Full scientific understanding of their properties has enabled us to develop them into practical system sorts. ・Quicksort honored as one of top 10 algorithms of 20th century in science and engineering. Mergesort. [last lecture] ... Quicksort. [this lecture] ... 2 Quicksort t-shirt 3 A brief history Tony Hoare. ・Invented quicksort to translate Russian into English. Learned Algol 60 (and recursion) to implement it. number). 9.9 X 10 45 is used to represent infinity. Imaginary conunent I and J are output variables, and A is the array (with values of x may not be negative and reM values of x may not be subscript bounds M:N) which is operated upon by this procedure. smaller than 1. ・ Partition takes the value X of a random element of the array A, Values of Qd~'(x) may be calculated easily by hypergeometric and rearranges the values of the elements of the array in such a series if x is not too small nor (n - m) too large. Q~m(x) can be way that there exist integers I and J with the following properties : computed from an appropriate set of values of Pnm(X) if X is near M _-< J < I =< NprovidedM < N 1.0 or ix is near 0. Loss of significant digits occurs for x as small as A[R] =< XforM =< R _-< J 1.1 if n is larger than 10. Loss of significant digits is a major diffi- A[R] = XforJ < R < I culty in using finite polynomiM representations also if n is larger A[R] ~ Xfor I =< R ~ N than m. However, QLEG has been tested in regions of x and n The procedure uses an integer procedure random (M,N) which both large and small; chooses equiprobably a random integer F between M and N, and procedure QLEG(m, nmax, x, ri, R, Q); value In, nmax, x, ri; also a procedure exchange, which exchanges the values of its two real In, mnax, x, ri; real array R, Q; parameters ; begin real t, i, n, q0, s; begin real X; integer F; n := 20; F := random (M,N); X := A[F]; if nmax > 13 then I:=M; J:=N; n := nmax +7; up: for I : = I step 1 until N do if ri = 0then if X < A [I] then go to down; begin ifm = 0then I:=N; Tony Hoare Q[0] := 0.5 X 10g((x + 1)/(x - 1)) down: forJ := J step --1 until M do else if A[J]<X then go to change; begin t := --1.0/sqrt(x X x-- 1); J:=M; rithms, and we can learn from that experience to separate q0 := 0; change: if I < J then begin exchange (A[IL A[J]); good algorithms from bad ones. Third, if the tile fits into Q[O] : = t; I := I+ 1;J:= J - 1; the memory of the computer, there is one algorithm, 1980 Turing Award for i : = 1 step 1 until m do go to up called Quicksort, which has been shown to perform well begin s := (x+x)X(i-1)Xt end 2: begin 4 in a variety of situations. Not only is this algorithm × Q[0]+ (3i-i× i-2)× q0; else if [ < F then begin exchange (A[IL A[F]) i if c >= 0 then Programming S. L. Graham, R. L. Rivest q0 := Q[0]; I:=I+l 3:begin Techniques Editors simpler than many otherActa sorting Informatica algorithms, but 7,empir- 327--355 (1977) Q[0] := s end end; end e:= aXe;f := bXd;goto8 ical [2, ll, 13, 21] and 9 analytic by Springer-Verlag [9] studies show that 1977 if x = 1 then else if F < J tllen begin exchange (A[F], A[J]) ; end 3 ; Implementing Quicksort can be expected to be up to twice as fast as its Q[0] := 9.9 I" 45; J:=J-1 e:=bXc; nearest competitors. The method is simple enough to be R[n + 1] := x - sqrt(x X x - 1); end ; ifd ~ 0then learned by programmers who have no previous experi- for i := n step --1 until 1 do end partition 4: begin Quicksort Programs ence with sorting, and those who do know other sorting R[i] := (i + m)/((i + i + 1) X x f:=bXd; goto8 methods should also find it profitable to learn about +(m-i- 1) X R[i+l]); end 4; Robert Sedgewick ALGORITHM 61 go to the end; f:=aXd; goto8Brown University Quicksort. if m = 0 then ALGORITHMPROCEDURES 64FOR RANGE ARITHMETIC 5: end 2; Because of its prominence, it is appropriate toThe study Analysis of Quicksort Programs* begin if x < 0.5 tben QUICKSORTALLAN GIBB* ifb > 0 then how Quicksort might be improved. This subject has Q[0] := arctan(x) - 1.5707963 else UniversityC. A. R. HOAREof Alberta, Calgary, Alberta, Canada 6: begin received considerable attention (see, for example, [1, 4, Q[0] := - aretan(1/x)end else This paper is a practical study of how to implement Robert Sedgewick Elliottbegin Brothers Ltd., Borehamwood, Hertfordshire, Eng. if d > 0 then 11, 13, 14, 18, 20]), but few real improvements have been begin t := 1/sqrt(x X x + 1); begin the Quicksort sorting algorithm and its best variants on procedure RANGESUM (a, b, c, d, e, f); suggested beyond those described by C.A.R. Hoare, the q0 := 0; procedure quicksort (A,M,N); value M,N; e := MIN(a X reald, b computers,X c); including how to apply various code real a,b,c,d,e,f; q[0] := t; array A; integer M,N; f:= MAX(a X c,b X d); go to8 inventor of Quicksort, in his original papers [5, 6]. Hoare Received January 19, t976 comment The term "range number" was used by P. S. Dwyer, optimization techniques. A detailed implementation for i : = 2 step 1 until m do comment Quicksort is a very fast and convenient method of end 6; also showed how to analyze Quicksort and predict its Linear Computations (Wiley, 1951). Machine procedures for combining the most effective improvements to begin s := (x + x) X (i -- 1) X t X Q[0I sorting an array in the random-access store of a computer. The e:= bX c; f := aX c; go to8 running time. The analysis has since been extended to range arithmetic were developed about 1958 by Ramon Moore, Quicksort is given, along with a discussion of how to +(3i+iX i -- 2) × q0; entire contents of the store may be sorted, since no extra space is end 5; Summary. The Quicksort sorting algorithm and its best variants are presented "Automatic Error Analysis in Digital Computation," LMSD implement it in assembly language. Analytic results the improvements that he suggested, and used to indicate qO := Q[0]; required. The average number of comparisons made is 2(M--N) In f:=aXc; Report 48421, 28 Jan. 1959, Lockheed Missiles and Space Divi- how they may best beand implemented analyzed. [9, 15, Results 17]. The are derived which make it possible to obtain exact formulas de- Q[0] := s end end; (N--M), and the average nmnber of exchanges is one sixth this if d _-< O then describing the performance of the programs are sion, Palo Alto, California, 59 pp. If a _< x -< b and c ~ y ~ d, subject of the careful implementation of Quicksort has R[n + 1] := x - sqrt(x × x + 1); amount. Suitable refinements of this method will be desirable for 7: begin summarized. A variety of special situations are scribing the total expected running time of particular implementations on real com- then RANGESUM yields an interval [e, f] such that e =< (x + y) not been studied as widely as global improvements to for i := n step - 1 until 1 do its implementation on any actual computer; e:=bXd; goto8considered from a practical standpoint to illustrate f. Because of machine operation (truncation or rounding) the puters of Quick,sort and an improvement called the median-of-three modification. R[i] := (i + m)/((i -- m + 1) × R[i + 1] begin integer 1,J ; end 7 ; the algorithm, but the savings to be realized are as machine sums a -4- c and b -4- d may not provide safe end-points Quicksort's wide applicability as an internal sorting Detailed analysis of the effect of an implementation technique called loop unwrapping --(i+i+ 1) X x); if M < N then begin partition (A,M,N,I,J); e:=aXd; significant. The history of Quicksort is quite complex, of the output interval. Thus RANGESUM requires a non-local method which requires negligible extra storage. for i : = 1 step 2 until nmax do quicksort (A,M,J) ; 8: e := ADJUSTPROD (e, -1); is presented. The paper is intended not only to present results of direct practical utility, real procedure ADJUSTSUM which will compensate for the Key Words and Phrases: Quicksort, analysis of and [15] contains a full survey of the many variants Rill := -- Rill; quicksort (A, I, N) f := ADJUSTPROD (f, 1) machine arithmetic. The body of ADJUSTSUM will be de- which, have been proposed.but also to illustrate the intriguing mathematics which arises in the complete analysis the: for i : = 1 step 1 until nnmx do end end RANGEMPY; algorithms, code optimization, sorting pendent upon the type of machine for which it is written and so The purpose of this paper is to describe in detail how Q[i] := Q[i - 1] X R[i] end quieksort procedure RANGEDVD CR(a, b, Categories: c, d, e, f) ; 4.0, 4.6, 5.25, 5.31, 5.5 of this important algorithm.