Abstract
It is well known that the performance of quicksort can be substantially improved by selecting the median of a sample of three elements as the pivot of each partitioning stage. This variant is easily generalized to samples of size s = 2k+1. For large samples the partitions are better as the median of the sample makes a more accurate estimate of the median of the array to be sorted, but the amount of additional comparisons and exchanges to find the median of the sample also increases.
We show that the optimal sample size to minimize the average total cost of quicksort (which includes both comparisons and exchanges) is s = a · √n+o(√n). We also give a closed expression for the constant factor a, which depends on the median-finding algorithm and the costs of elementary comparisons and exchanges. The result above holds in most situations, unless the cost of an exchange exceeds by far the cost of a comparison. In that particular case, it is better to select not the median of the samples, but the (p}+1)th element. The value of p can be precisely determined as a function of the ratio between the cost of an exchange and the cost of a comparison.
This research was supported by the ESPRIT LTR Project ALCOM-IT, contract # 20244 and by a grant from CIRIT (Comissió Interdepartamental de Recerca i Innovació Tecnológica).
Preview
Unable to display preview. Download preview PDF.
References
R.W. Floyd and R.L. Rivest. Expected time bounds for selection. Communications of the ACM, 18:165–173, 1975.
C.A.R. Hoare. Find (Algorithm 65). Communications of the ACM, 4:321–322, 1961.
C.A.R. Hoare. Quicksort. Computer Journal, 5:10–15, 1962.
D.E. Knuth. The Art of Computer Programming: Sorting and Searching, volume 3. Addison-Wesley, 1973.
C. Martínez and S. Roura. Optimal sampling strategies in quicksort and quickselect. Technical Report LSI-98-1-R, LSI-UPC, 1998. Also available from http://www.lsi.upc.es/dept/techreps/1998.html.
C.C. McGeoch and J.D. Tygar. Optimal sampling strategies for quicksort. Random Structures & Algorithms, 7:287–300, 1995.
S. Roura. An improved master theorem for divide-and-conquer recurrences. In P. Degano, R. Gorrieri, and A. Marchetti-Spaccamela, editors, Proc. of the 24th Int. Colloquium on Automata, Languages and Programming (ICALP'97), volume 1256 of Lecture Notes in Computer Science. Springer, 1997.
R. Sedgewick. Quicksort. Garland, 1978.
R. Sedgewick. Algorithms in C, volume 1. Addison-Wesley, 3rd edition, 1997.
R.C. Singleton. Algorithm 347: An efficient algorithm for sorting with minimal storage. Communications of the ACM, 12:185–187, 1969.
M.H. van Emden. Increasing the efficiency of quicksort. Communications of the ACM, 13:563–567, 1970.
Author information
Authors and Affiliations
Editor information
Rights and permissions
Copyright information
© 1998 Springer-Verlag Berlin Heidelberg
About this paper
Cite this paper
Martínez, C., Roura, S. (1998). Optimal sampling strategies in quicksort. In: Larsen, K.G., Skyum, S., Winskel, G. (eds) Automata, Languages and Programming. ICALP 1998. Lecture Notes in Computer Science, vol 1443. Springer, Berlin, Heidelberg. https://doi.org/10.1007/BFb0055064
Download citation
DOI: https://doi.org/10.1007/BFb0055064
Published:
Publisher Name: Springer, Berlin, Heidelberg
Print ISBN: 978-3-540-64781-2
Online ISBN: 978-3-540-68681-1
eBook Packages: Springer Book Archive