Optimal Sampling Strategies in Quicksort and Quickselect
Abstract
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.
[2] J. Bentley, Programming Pearls, Addison‐Wesley, Reading, MA, 1986.
[3] , Engineering a sort function, Software—Practice and Experience, 23 (1993), pp. 1249–1265. avn SPEXBL 0038-0644 Softw.: Pract. Exp.
[4] , Expected time bounds for selection, Comm. ACM, 18 (1975), pp. 165–173. cao CACMA2 0001-0782 Commun. ACM
[5] , 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
[6] , On the median‐of‐k version of Hoare’s selection algorithm, Theor. Inform. Appl., 33 (1999), 177–192 2000i:68036
[7] , Algorithm 65: Find, Commu. ACM, 4 (1961), pp. 321–322. cao CACMA2 0001-0782 Commun. ACM
[8] , Quicksort, Computer Journal, 5 (1962), pp. 10–15. coj CMPJA6 0010-4620 Comput. J.
[9] , Comparisons in Hoare’s Find algorithm, Combin. Probab. Comput., 7 (1998), 111–120 98j:68016
[10] , 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
[11] , Mathematical analysis of algorithms, North‐Holland, Amsterdam, 1972, 19–27 53:7122
[12] , 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
[13] H. Mahmoud, Sorting: A Distribution Theory, John Wiley and Sons, New York, 2000.
[14] , Analysis of QUICKSELECT: an algorithm for order statistics, RAIRO Inform. Théor. Appl., 29 (1995), 255–276 96h:68040
[15] , Optimal sampling strategies in quicksort, Lecture Notes in Comput. Sci., Vol. 1443, Springer, Berlin, 1998, 327–338 99m:68038
[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.
[17] , Optimal sampling strategies for Quicksort, Random Structures Algorithms, 7 (1995), 287–300 96m:68038
[18] , 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
[19] , Multiple Quickselect—Hoare’s Find algorithm for several elements, Inform. Process. Lett., 56 (1995), 123–129 96h:68041
[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.
[21] , Improved master theorems for divide‐and‐conquer recurrences, J. ACM, 48 (2001), pp. 170–205. abz ZZZZZZ 1535-9921 J. ACM
[22] , The analysis of Quicksort programs, Acta Informat., 7 (1976/77), 327–355 56:10127
[23] , Implementing quicksort programs, Comm. ACM, 21 (1978), pp. 847–856. cao CACMA2 0001-0782 Commun. ACM
[24] R. Sedgewick, Quicksort, Garland, New York, 1978.
[25] , Algorithm 347: An efficient algorithm for sorting with minimal storage, Comm. ACM, 12 (1969), pp. 185–187. cao CACMA2 0001-0782 Commun. ACM
[26] , Increasing the efficiency of quicksort, Comm. ACM, 13 (1970), pp. 563–567. cao CACMA2 0001-0782 Commun. ACM

