It is well known that the performance of quicksort can be improved by selecting the median of a sample of elements as the pivot of each partitioning stage. For large samples the partitions are better, but the amount of additional comparisons and exchanges to find the median of the sample also increases. We show in this paper that the optimal sample size to minimize the average total cost of quicksort, as a function of the size n of the current subarray size, is $a\cdot \sqrt{n} + o(\sqrt{n}\,)$. We give a closed expression for a, which depends on the selection algorithm and the costs of elementary comparisons and exchanges. Moreover, we show that selecting the medians of the samples as pivots is not the best strategy when exchanges are much more expensive than comparisons. We also apply the same ideas and techniques to the analysis of quickselect and get similar results.

  • [1]  M. Abramowitz and I. Stegun, eds., Handbook of Mathematical Functions, Dover, New York, 1964. Google Scholar

  • [2]  J. Bentley, Programming Pearls, Addison‐Wesley, Reading, MA, 1986. Google Scholar

  • [3]  J. Bentley and  and M. McIlroy, Engineering a sort function, Software—Practice and Experience, 23 (1993), pp. 1249–1265. avn SPEXBL 0038-0644 Softw.: Pract. Exp. CrossrefISIGoogle Scholar

  • [4]  R. Floyd and  and R. Rivest, Expected time bounds for selection, Comm. ACM, 18 (1975), pp. 165–173. cao CACMA2 0001-0782 Commun. ACM CrossrefISIGoogle Scholar

  • [5]  Ronald Graham, , Donald Knuth and , Oren Patashnik, Matematyka konkretna, Wydawnictwo Naukowe PWN, Warsaw, 1998, 719–0, Translated from the second English (1994) edition by P. Chrząstowski, A. Czumaj, L. Gąsieniec and M. Raczunas 99m:68002 Google Scholar

  • [6]  Rudolf Grübel, On the median‐of‐k version of Hoare’s selection algorithm, Theor. Inform. Appl., 33 (1999), 177–192 2000i:68036 CrossrefISIGoogle Scholar

  • [7]  C. Hoare, Algorithm 65: Find, Commu. ACM, 4 (1961), pp. 321–322. cao CACMA2 0001-0782 Commun. ACM CrossrefGoogle Scholar

  • [8]  C. Hoare, Quicksort, Computer Journal, 5 (1962), pp. 10–15. coj CMPJA6 0010-4620 Comput. J. CrossrefISIGoogle Scholar

  • [9]  Peter Kirschenhofer and , Helmut Prodinger, Comparisons in Hoare’s Find algorithm, Combin. Probab. Comput., 7 (1998), 111–120 98j:68016 CrossrefISIGoogle Scholar

  • [10]  P. Kirschenhofer, , H. Prodinger and , C. Martínez, Analysis of Hoare’s FIND algorithm with median‐of‐three partition, Random Structures Algorithms, 10 (1997), 143–156, Average‐case analysis of algorithms (Dagstuhl, 1995) 99c:68063 CrossrefISIGoogle Scholar

  • [11]  Donald Knuth, Mathematical analysis of algorithms, North‐Holland, Amsterdam, 1972, 19–27 53:7122 Google Scholar

  • [12]  Donald Knuth, The art of computer programming. Volume 3, Addison‐Wesley Publishing Co., Reading, Mass.‐London‐Don Mills, Ont., 1973xi+722 pp. (1 foldout), Sorting and searching; Addison‐Wesley Series in Computer Science and Information Processing 56:4281 Google Scholar

  • [13]  H. Mahmoud, Sorting: A Distribution Theory, John Wiley and Sons, New York, 2000. Google Scholar

  • [14]  Hosam Mahmoud, , Reza Modarres and , Robert Smythe, Analysis of QUICKSELECT: an algorithm for order statistics, RAIRO Inform. Théor. Appl., 29 (1995), 255–276 96h:68040 CrossrefISIGoogle Scholar

  • [15]  Conrado Martínez and , Salvador Roura, Optimal sampling strategies in quicksort, Lecture Notes in Comput. Sci., Vol. 1443, Springer, Berlin, 1998, 327–338 99m:68038 Google Scholar

  • [16]  C. Martínez and S. Roura, Optimal Sampling Strategies in Quicksort and Quickselect, Tech. Rep. LSI‐98‐1‐R, LSI‐UPC, 1998; also available online from www.lsi.upc.es/dept/techreps/1998.html. Google Scholar

  • [17]  C. McGeoch and , J. Tygar, Optimal sampling strategies for Quicksort, Random Structures Algorithms, 7 (1995), 287–300 96m:68038 CrossrefISIGoogle Scholar

  • [18]  Alois Panholzer and , Helmut Prodinger, A generating functions approach for the analysis of grand averages for multiple QUICKSELECT, Proceedings of the Eighth International Conference “Random Structures and Algorithms” (Poznan, 1997), Vol. 13, 1998, 189–209 99k:68038 Google Scholar

  • [19]  Helmut Prodinger, Multiple Quickselect—Hoare’s Find algorithm for several elements, Inform. Process. Lett., 56 (1995), 123–129 96h:68041 CrossrefISIGoogle Scholar

  • [20]  S. Roura, Divide‐and‐Conquer Algorithms and Data Structures, Ph.D. thesis, Dept. Llenguatges i Sistemes Informàtics, Universitat Politècnica de Catalunya, Barcelona, Spain, 1997. Google Scholar

  • [21]  S. Roura, Improved master theorems for divide‐and‐conquer recurrences, J. ACM, 48 (2001), pp. 170–205. abz ZZZZZZ 1535-9921 J. ACM CrossrefISIGoogle Scholar

  • [22]  Robert Sedgewick, The analysis of Quicksort programs, Acta Informat., 7 (1976/77), 327–355 56:10127 CrossrefISIGoogle Scholar

  • [23]  R. Sedgewick, Implementing quicksort programs, Comm. ACM, 21 (1978), pp. 847–856. cao CACMA2 0001-0782 Commun. ACM CrossrefISIGoogle Scholar

  • [24]  R. Sedgewick, Quicksort, Garland, New York, 1978. Google Scholar

  • [25]  R. Singleton, Algorithm 347: An efficient algorithm for sorting with minimal storage, Comm. ACM, 12 (1969), pp. 185–187. cao CACMA2 0001-0782 Commun. ACM CrossrefISIGoogle Scholar

  • [26]  M. van Emden, Increasing the efficiency of quicksort, Comm. ACM, 13 (1970), pp. 563–567. cao CACMA2 0001-0782 Commun. ACM CrossrefISIGoogle Scholar