Skip to main content

Optimal sampling strategies in quicksort

  • Conference paper
  • First Online:
Automata, Languages and Programming (ICALP 1998)

Part of the book series: Lecture Notes in Computer Science ((LNCS,volume 1443))

Included in the following conference series:

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).

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Institutional subscriptions

Preview

Unable to display preview. Download preview PDF.

Unable to display preview. Download preview PDF.

References

  1. R.W. Floyd and R.L. Rivest. Expected time bounds for selection. Communications of the ACM, 18:165–173, 1975.

    Article  MATH  Google Scholar 

  2. C.A.R. Hoare. Find (Algorithm 65). Communications of the ACM, 4:321–322, 1961.

    Article  Google Scholar 

  3. C.A.R. Hoare. Quicksort. Computer Journal, 5:10–15, 1962.

    Article  MATH  MathSciNet  Google Scholar 

  4. D.E. Knuth. The Art of Computer Programming: Sorting and Searching, volume 3. Addison-Wesley, 1973.

    Google Scholar 

  5. 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.

    Google Scholar 

  6. C.C. McGeoch and J.D. Tygar. Optimal sampling strategies for quicksort. Random Structures & Algorithms, 7:287–300, 1995.

    MATH  MathSciNet  Google Scholar 

  7. 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.

    Google Scholar 

  8. R. Sedgewick. Quicksort. Garland, 1978.

    Google Scholar 

  9. R. Sedgewick. Algorithms in C, volume 1. Addison-Wesley, 3rd edition, 1997.

    Google Scholar 

  10. R.C. Singleton. Algorithm 347: An efficient algorithm for sorting with minimal storage. Communications of the ACM, 12:185–187, 1969.

    Article  Google Scholar 

  11. M.H. van Emden. Increasing the efficiency of quicksort. Communications of the ACM, 13:563–567, 1970.

    Article  MATH  Google Scholar 

Download references

Author information

Authors and Affiliations

Authors

Editor information

Kim G. Larsen Sven Skyum Glynn Winskel

Rights and permissions

Reprints 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

Publish with us

Policies and ethics