Comparison of Sorting algorithms ppt

data structures sorting algorithms ppt and sorting algorithms and their complexities ppt and time complexity of sorting algorithms ppt
OscarTucker Profile Pic
Published Date:19-07-2017
Your Website URL(Optional)
ROBERT SEDGEWICK KEVIN WAYNE Algorithms 2.3 QUICKSORT quicksort ‣ selection ‣ duplicate keys ‣ Algorithms FOUR TH EDITION system sorts ‣ ROBERT SEDGEWICK KEVIN WAYNE Last updated on 3/29/17 9:02 AMTwo 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. th Quicksort honored as one of top 10 algorithms of 20 century
 in science and engineering. 
 Mergesort. last lecture 
 Quicksort. this lecture ... 2Quicksort t-shirt 3Quicksort t-shirt 42.3 QUICKSORT quicksort ‣ selection ‣ duplicate keys ‣ Algorithms system sorts ‣ ROBERT SEDGEWICK KEVIN WAYNE http://algs4.cs.princeton.eduQuicksort overview Step 1. Shuffle the array. Step 2. Partition the array so that, for some j Entry aj is in place. No larger entry to the left of j. No smaller entry to the right of j. Step 3. Sort each subarray recursively. input Q U I C K S O R T E X A M P L E shuffle K R A T E L E P U I M Q C X O S partitioning item partition E C A I E K L P U T M Q R X O S not greater not less sort left A C E E I K L P U T M Q R X O S sort right A C E E I K L M O P Q R S T U X result A C E E I K L M O P Q R S T U X Quicksort overview 6number). 9.9 X 10 45 is used to represent infinity. Imaginary conunent I and J are output variables, and A is the array (with Tony Hoare 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. Qm(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 AR = XforM = R _- J 1.1 if n is larger than 10. Loss of significant digits is a major diffi- AR = XforJ R I Invented quicksort to translate Russian into English. culty in using finite polynomiM representations also if n is larger AR 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 but couldn't explain his algorithm or implement it 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; Learned Algol 60 (and recursion). n := 20; F := random (M,N); X := AF; 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; Implemented quicksort. begin ifm = 0then I:=N; Q0 := 0.5 X 10g((x + 1)/(x - 1)) down: forJ := J step 1 until M do else if AJX then go to change; begin t := 1.0/sqrt(x X x 1); J:=M; q0 := 0; Tony Hoare
 change: if I J then begin exchange (AIL AJ); QO : = t; I := I+ 1;J:= J - 1; for i : = 1 step 1 until m do 1980 Turing Award go to up begin s := (x+x)X(i-1)Xt end 2: begin 4 ×Q0+ (3i-i×i-2)×q0; else if F then begin exchange (AIL AF) i if c = 0 then q0 := Q0; I:=I+l 3:begin Q0 := s end end; end e:= aXe;f := bXd;goto8 if x = 1 then else if F J tllen begin exchange (AF, AJ) ; end 3 ; Q0 := 9.9 I" 45; J:=J-1 e:=bXc; Rn + 1 := x - sqrt(x X x - 1); end ; ifd 0then for i := n step 1 until 1 do end partition 4: begin Ri := (i + m)/((i + i + 1) X x f:=bXd; goto8 +(m-i- 1) X Ri+l); end 4; ALGORITHM 61 go to the end; f:=aXd; goto8 PROCEDURES FOR RANGE ARITHMETIC ALGORITHM 64 if m = 0 then 5: end 2; ALLAN GIBB begin if x 0.5 tben QUICKSORT ifb 0 then Q0 := arctan(x) - 1.5707963 else 6: begin University of Alberta, Calgary, Alberta, Canada C. A. R. HOARE Q0 := - aretan(1/x)end else if d 0 then Elliott Brothers Ltd., Borehamwood, Hertfordshire, Eng. begin begin t := 1/sqrt(x X x + 1); begin procedure RANGESUM (a, b, c, d, e, f); q0 := 0; procedure quicksort (A,M,N); value M,N; e := MIN(a X d, b X c); real a,b,c,d,e,f; q0 := t; array A; integer M,N; f:= MAX(a X c,b X d); go to8 comment The term "range number" was used by P. S. Dwyer, for i : = 2 step 1 until m do comment Quicksort is a very fast and convenient method of end 6; Linear Computations (Wiley, 1951). Machine procedures for begin s := (x + x) X (i 1) X t X Q0I sorting an array in the random-access store of a computer. The e:= bX c; f := aX c; go to8 range arithmetic were developed about 1958 by Ramon Moore, +(3i+iX i 2) × q0; entire contents of the store may be sorted, since no extra space is end 5; "Automatic Error Analysis in Digital Computation," LMSD qO := Q0; required. The average number of comparisons made is 2(MN) In f:=aXc; Report 48421, 28 Jan. 1959, Lockheed Missiles and Space Divi- Q0 := s end end; (NM), and the average nmnber of exchanges is one sixth this if d _- O then sion, Palo Alto, California, 59 pp. If a _ x - b and c y d, Rn + 1 := x - sqrt(x × x + 1); amount. Suitable refinements of this method will be desirable for 7: begin then RANGESUM yields an interval e, f such that e = (x + y) for i := n step - 1 until 1 do its implementation on any actual computer; e:=bXd; goto8 f. Because of machine operation (truncation or rounding) the Ri := (i + m)/((i m + 1) × Ri + 1 begin integer 1,J ; end 7 ; machine sums a -4- c and b -4- d may not provide safe end-points (i+i+ 1) X x); if M N then begin partition (A,M,N,I,J); e:=aXd; of the output interval. Thus RANGESUM requires a non-local for i : = 1 step 2 until nmax do quicksort (A,M,J) ; 8: e := ADJUSTPROD (e, -1); real procedure ADJUSTSUM which will compensate for the Rill := Rill; quicksort (A, I, N) f := ADJUSTPROD (f, 1) machine arithmetic. The body of ADJUSTSUM will be de- the: for i : = 1 step 1 until nnmx do end end RANGEMPY; pendent upon the type of machine for which it is written and so Qi := Qi - 1 X Ri end quieksort procedure RANGEDVD (a, b, c, d, e, f) ; is not given here. (An example, however, appears below.) It end QLEG; real a, b, c, d, e, f; is assumed that ADJUSTSUM has as parameters real v and w, comment If the range divisor includes zero the program This procedure was developed in part under the sponsorship and integer i, and is accompanied by a non-local real procedure exists to a non-local label "zerodvsr". RANGEDVD assumes a of the Air Force Cambridge Research Center. CORRECTION which gives an upper bound to the magnitude ALGORITHM 65 Communications of the ACM (July 1961) non-local real procedure ADJUSTQUOT which is analogous of the error involved in the machine representation of a number. FIND (possibly identical) to ADJUSTPROD; The output ADJUSTSUM provides the left end-point of the 7 begin C. A. R. HOARE output interval of RANGESUM when ADJUSTSUM is called ALGORITHM 63 if c = 0 A d 0 then go to zer0dvsr; Elliott Brothers Ltd., Borehamwood, Hertfordshire, Eng. with i = 1, and the right end-point when called with i = 1 if c 0 then PARTITION The procedures RANGESUB, RANGEMPY, and RANGEDVD procedure find (A,M,N,K); value M,N,K; 1: begin C. A. R. HOARE provide for the remaining fundamental operations in range array A; integer M,N,K; ifb 0 then arithmetic. RANGESQR gives an interval within which the Elliott Brothers Ltd., Borehamwood, Hertfordshire, Eng. comment Find will assign to A K the value which it would 2: begin square of a range nmnber must lie. RNGSUMC, RNGSUBC, procedure partition (A,M,N,I,J); value M,N; have if the array A M:N had been sorted. The array A will be e := b/d; go to3 RNGMPYC and RNGDVDC provide for range arithmetic with array A; integer M,N,1,J; partly sorted, and subsequent entries will be faster than the first; end 2; complex range arguments, i.e. the real and imaginary parts e := b/c; are range numbers Communications of the ACM 321 3: ifa _ 0then begin 4: begin e := ADJUSTSUM (a, c, -1); f := a/c; go to 8 f := ADJUSTSUM (b, d, 1) end 4; end RANGESUM; f := a/d; go to 8 procedure RANGESUB (a, b, c, d, e, f) ; end 1 ; real a, b,c, d,e, f; if a 0 then comment RANGESUM is a non-local procedure; 5: begin begin e := a/c; go to6 RANGESUM (a, b, -d, c, e, f) end 5 ; end RANGESUB ; e := a/d; procedure RANGEMPY (a, b, c, d, e, f); 6: ifb 0then real a, b, c, d, e, f; 7: begin comment ADJUSTPROD, which appears at the end of this f := b/c; go to8 procedure, is analogous to ADJUSTSUM above and is a non- end 7 ; local real procedure. MAX and MIN find the maximum and f := b/d; minimum of a set of real numbers and are non-local; 8: e := ADJUSTQUOT (e, -1); f := ADJUSTQUOT (f,1) begin end RANGEDVD ; real v, w; procedure RANGESQR (a, b, e, f); if a 0A c = 0then real a, b, e, f; 1: begin v:=c; c:=a; a:=v; w:=d; d:=b; b:=w comment ADJUSTPROD is a non-10cal procedure; end 1; begin if a = O then if a 0 then Communications of the &CM 319 Tony Hoare Invented quicksort to translate Russian into English. but couldn't explain his algorithm or implement it Learned Algol 60 (and recursion). Implemented quicksort. Tony Hoare
 1980 Turing Award “ There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult. ” “ I call it my billion-dollar mistake. It was the invention of the null reference in 1965… This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years. ” 8Bob Sedgewick Refined and popularized quicksort. Analyzed many versions of quicksort. rithms, and we can learn from that experience to separate Bob Sedgewick good algorithms from bad ones. Third, if the tile fits into the memory of the computer, there is one algorithm, called Quicksort, which has been shown to perform well in a variety of situations. Not only is this algorithm Programming S. L. Graham, R. L. Rivest simpler than many other sorting algorithms, but empir- Techniques Editors ical 2, ll, 13, 21 and analytic 9 studies show that Quicksort can be expected to be up to twice as fast as its Implementing nearest competitors. The method is simple enough to be Acta Informatica 7, 327355 (1977) learned by programmers who have no previous experi- Quicksort Programs 9 by Springer-Verlag 1977 ence with sorting, and those who do know other sorting methods should also find it profitable to learn about Robert Sedgewick Quicksort. Brown University Because of its prominence, it is appropriate to study how Quicksort might be improved. This subject has The Analysis of Quicksort Programs received considerable attention (see, for example, 1, 4, This paper is a practical study of how to implement 11, 13, 14, 18, 20), but few real improvements have been Robert Sedgewick the Quicksort sorting algorithm and its best variants on suggested beyond those described by C.A.R. Hoare, the real computers, including how to apply various code Received January 19, t976 inventor of Quicksort, in his original papers 5, 6. Hoare optimization techniques. A detailed implementation also showed how to analyze Quicksort and predict its combining the most effective improvements to Summary. The Quicksort sorting algorithm and its best variants are presented running time. The analysis has since been extended to Quicksort is given, along with a discussion of how to and analyzed. Results are derived which make it possible to obtain exact formulas de- the improvements that he suggested, and used to indicate implement it in assembly language. Analytic results scribing the total expected running time of particular implementations on real com- how they may best be implemented 9, 15, 17. The describing the performance of the programs are puters of Quick, sort and an improvement called the median-of-three modification. subject of the careful implementation of Quicksort has Detailed analysis of the effect of an implementation technique called loop unwrapping summarized. A variety of special situations are not been studied as widely as global improvements to is presented. The paper is intended not only to present results of direct practical utility, considered from a practical standpoint to illustrate the algorithm, but but the also savings to illustrate to be the realized intriguing are as mathematics which arises in the complete analysis Quicksort's wide applicability as an internal sorting of this important algorithm. significant. The history of Quicksort is quite complex, method which requires negligible extra storage. and 15 contains a full survey of the many variants Key Words and Phrases: Quicksort, analysis of which, have been proposed. 1. Introduction algorithms, code optimization, sorting The purpose of this paper is to describe in detail how CR Categories: 4.0, 4.6, 5.25, 5.31, 5.5 In t96t-62 C.A.R. Hoare presented a new algorithm called Quicksort 7, 8 Quicksort can best be implemented to handle actual which is suitable for putting files into order by computer. This method combines applications on real computers. A general description of elegance and efficiency, and it remains today the most useful general-purpose the algorithm is followed by descriptions of the most sorting method for computers. The practical utility of the algorithm has meant effective improvements that have been proposed (as 9 not only that it has been sfibjected to countless modifications (though few real demonstrated in 15). Next, an implementation of improvements have been suggested beyond those described by Hoare), but also Quicksort in a typical high level language is presented, Introduction and assembly language that implementation it has been used issues .in are countless con- applications, often to sort very large, files. sidered. This discussion should easily translate to real Consequently, it is important to be able to estimate how long an implementation One of the most widely studied practical problems in languages on real machines. Finally, a number of special of Quicksort can be expected to run, in order to be able to compare variants or computer science is sorting: the use of a computer to put issues are considered which may be of importance in estimate expenses. Fortunately, as we shall see, this is an algorithm which can be files in order. A person wishing to use a computer to sort particular sorting applications. analyzed. (Hoare recognized this, and gave some analytic results in 8.) It is is faced with the problem of determining which of the This paper is intended to be a self-contained overview possible to derive exact formulas describing the average performance of real many available algorithms is best suited for his purpose. of the properties of Quicksort for use by those who need implementations of the algorithm. This task is becoming less difficult than it once was for to actually implement and use the algorithm. A compan- The history of Quicksort is quite complex, and a full survey of the many variants three reasons. First, sorting is an area in which the ion paper 17 provides the analytical results which su- which have been proposed is given in t 7. In addition, t 7 gives analytic results mathematical analysis of algorithms has been particu- port much of the discussion presented here. describing many of the improvements which have been suggested for the purpose larly successful: we can predict the performance of many of determining which are the most effective. There are many examples in 7 sorting methods and compare them intelligently. Second, which illustrate that the simplicity of Quicksort is deceiving. The algorithm has we have a great deal of experience using sorting algo- The Algofithm hidden subtleties which can have significant effects on performance. Furthermore, Permission to copy without fee all or part of this material is as we shall see, simple changes to the algorithm or its implementation can radically granted provided that the copies are not made or distributed for direct Quicksort is a recursive method for sorting an array commercial advantage, the ACM copyright notice and the title of the change the analysis. In this paper, we shall consider in detail how practical A1, A2 ..... AN by first "partitioning" it so that the publication and its date appear, and notice is given that copying is by implementations of the best versions of Quicksort may be analyzed. following conditions hold: permission of the Association for Computing Machinery. To copy In this paper, we will deal with the analysis of: (i) the basic Quicksort algo- otherwise, or to republish, requires a fee and/or specific permission. (i) Some key v is in its final position in the array. (If it This work was supported in part by the Fannie and John Hertz rithm; (ii) an improvement called the "median-of-three" modification which is thejth smallest, it is in position Aj.) Foundation and in part by NSF Grants. No. GJ-28074 and MCS75- 23738. reduces the average number of comparisons required; and (iii) an implementation (ii) All elements to the left of Aj are less than or equal Author's address: Division of Applied Mathematics and Computer technique called "loop unwrapping" which reduces the amount of overhead per to it. (These elements A 1 , A 2 ..... A j - 1 are Science Program, Brown University, Providence, RI 02912. comparison. These particular methods not only represent the most effective vari- © 1978 ACM 0001-0782/78/1000-0847 00.75 called the "left subtile.") This work was supported in part by the Fannie and John Hertz Foundation, and 847 Communications October 1978 in part by the National Science Foundation Grants No. GJ-28074 and MCS75-23738. of Volume 21 the ACM Number 10 22 Acta Informatica, Vol. 7 Quicksort partitioning demo Repeat until i and j pointers cross. Scan i from left to right so long as (ai alo). Scan j from right to left so long as (aj alo). Exchange ai with aj. K R A T E L E P U I M Q C X O S lo i j 10Quicksort partitioning demo Repeat until i and j pointers cross. Scan i from left to right so long as (ai alo). Scan j from right to left so long as (aj alo). Exchange ai with aj. When pointers cross. Exchange alo with aj. E C A I E K L P U T M Q R X O S lo j hi partitioned 11The music of quicksort partitioning (by Brad Lyon) 12Quicksort: Java code for partitioning private static int partition(Comparable a, int lo, int hi) int i = lo, j = hi+1; while (true) while (less(a++i, alo)) find item on left to swap if (i == hi) break; while (less(alo, aj)) find item on right to swap if (j == lo) break; if (i = j) break; check if pointers cross exch(a, i, j); swap exch(a, lo, j); swap with partitioning item before v return j; return index of item now known to be in place lo hi during " v v v before v lo hi i j after v " v v during before " v v v v lo j hi lo hi i j after Quicksort partitioning overview during " v v v " v v v 13 lo j hi i j after Quicksort partitioning overview v " v v lo j hi Quicksort partitioning overviewQuicksort quiz 1 How many compares to partition an array of length n ? A. ¼ n B. ½ n C. n D. n lg n E. I don't know. scan until ≤ M scan until ≥ M M A B C D E V W X Y Z 0 1 2 3 4 5 6 7 8 9 10 n+1 compares in worst case (E and V are compared with M twice) 14Quicksort: Java implementation public class Quick private static int partition(Comparable a, int lo, int hi) / see previous slide / public static void sort(Comparable a) shuffle needed for StdRandom.shuffle(a); performance guarantee sort(a, 0, a.length - 1); (stay tuned) private static void sort(Comparable a, int lo, int hi) if (hi = lo) return; int j = partition(a, lo, hi); sort(a, lo, j-1); sort(a, j+1, hi); 15Quicksort trace lo j hi 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 initial values Q U I C K S O R T E X A M P L E random shuffle K R A T E L E P U I M Q C X O S 0 5 15 E C A I E K L P U T M Q R X O S 0 3 4 E C A E I K L P U T M Q R X O S 0 2 2 A C E E I K L P U T M Q R X O S 0 0 1 A C E E I K L P U T M Q R X O S 1 1 A C E E I K L P U T M Q R X O S 4 4 A C E E I K L P U T M Q R X O S 6 6 15 A C E E I K L P U T M Q R X O S no partition 7 9 15 A C E E I K L M O P T Q R X U S for subarrays 7 7 8 A C E E I K L M O P T Q R X U S of size 1 8 8 A C E E I K L M O P T Q R X U S 10 13 15 A C E E I K L M O P S Q R T U X 10 12 12 A C E E I K L M O P R Q S T U X 10 11 11 A C E E I K L M O P Q R S T U X 10 10 A C E E I K L M O P Q R S T U X 14 14 15 A C E E I K L M O P Q R S T U X 15 15 A C E E I K L M O P Q R S T U X result A C E E I K L M O P Q R S T U X Quicksort trace (array contents after each partition) 16Quicksort animation 50 random items algorithm position in order current subarray not in order 17Quicksort: implementation details Partitioning in-place. Using an extra array makes partitioning easier
 (and stable), but is not worth the cost. 
 Terminating the loop. Testing whether the pointers cross is trickier
 than it might seem. 
 Equal keys. When duplicates are present, it is (counter-intuitively)
 stay tuned better to stop scans on keys equal to the partitioning item's key. 
 Preserving randomness. Shuffling is needed for performance guarantee. Equivalent alternative. Pick a random partitioning item in each subarray. 18Quicksort: empirical analysis (1961) Running time estimates: Algol 60 implementation. National Elliott 405 computer. Elliott 405 magnetic disc sorting n 6-word items with 1-word keys (16K words) 19Quicksort: empirical analysis Running time estimates: 8 Home PC executes 10 compares/second. 12 Supercomputer executes 10 compares/second. 
 2 insertion sort (n ) mergesort (n log n) quicksort (n log n) 
 computer thousand million billion thousand million billion thousand million billion 
 317 home instant 2.8 hours instant 1 second 18 min instant 0.6 sec 12 min years 
 super instant 1 second 1 week instant instant instant instant instant instant 
 Lesson 1. Good algorithms are better than supercomputers. Lesson 2. Great algorithms are better than good ones. 20