Quicksort with Equal Keys
- 1 June 1977
- journal article
- Published by Society for Industrial & Applied Mathematics (SIAM) in SIAM Journal on Computing
- Vol. 6 (2), 240-267
- https://doi.org/10.1137/0206018
Abstract
This paper considers the problem of implementing and analyzinga Quicksortprogram when equal keys are likely to be present in the file to be sorted. Upper and lowerbounds are derivedon the average number of comparisons needed by any Quicksortprogramwhenequal keys are present. It is shown that, of the three strategies which have been suggested for dealing with equal keys, the method of always stopping the scanning pointers on keys equal to the partitioning element performs best. Introduction. The Quicksort algorithm, which was introduced by C. A. R. Hoare in 1960 (6), (7), has gained wide acceptance as the most efficient general-purpose sorting method suitable for useon computers. The algorithmhas a rich history: many modifications have been suggested to improve its perfor- mance, and exact formulas have been derived describing the time and space requirements of the most important variants (7), (9), (14). Although most files to be sorted contain at least some equal keys and sorting programs must always deal withthem properly, it is generally considered reasona- ble to assume in the analysis that the keys are distinct. This assumption is fundamental to the analysis of nearly all sorting programs, and it is very often realistic. In any situation where the number of possible key values far exceeds the number of keys to be sorted, the probability that equal keys are present will be very small. However, if the number of possible key values is not large, or if there is some other information about the file which indicates that equal keys are likely to be present, then the performance of many sorting programs, including Quicksort, has not been carefully studied. The purpose of this paper, then, is to investigate the performance of Quicksort when equal keys are present. The following section describes the algorithm and its analysis for distinct keys. Next, lower and upper bounds are derived for the average number of comparisons taken when equal keys are present. Following that, we shall consider, from a practical standpoint, the problem of implementing a version of Quicksort to handle equal keys. Finallywe shall compare the various methods and discover which is the most useful in practical sorting applications.Keywords
This publication has 7 references indexed in Scilit:
- Structured Programming with go to StatementsACM Computing Surveys, 1974
- Tree acceptors and some of their applicationsJournal of Computer and System Sciences, 1970
- Algorithm 347: an efficient algorithm for sorting with minimal storage [M1]Communications of the ACM, 1969
- Algorithm 271: quickersortCommunications of the ACM, 1965
- QuicksortThe Computer Journal, 1962
- Some Combinatorial Properties of Certain Trees With Applications to Searching and SortingJournal of the ACM, 1962
- Algorithm 64: QuicksortCommunications of the ACM, 1961