## Introspective Sorting and Selection Algorithms (1997)

Venue: | Software Practice and Experience |

Citations: | 38 - 1 self |

### BibTeX

@ARTICLE{Musser97introspectivesorting,

author = {David Musser},

title = {Introspective Sorting and Selection Algorithms},

journal = {Software Practice and Experience},

year = {1997},

volume = {27},

pages = {983--993}

}

### Years of Citing Articles

### OpenURL

### Abstract

Quicksort is the preferred in-place sorting algorithm in many contexts, since its average computing time on uniformly distributed inputs is \Theta(N log N) and it is in fact faster than most other sorting algorithms on most inputs. Its drawback is that its worst-case time bound is \Theta(N ). Previous attempts to protect against the worst case by improving the way quicksort chooses pivot elements for partitioning have increased the average computing time too much---one might as well use heapsort, which has a \Theta(N log N) worst-case time bound but is on the average 2 to 5 times slower than quicksort. A similar dilemma exists with selection algorithms (for finding the i-th largest element) based on partitioning. This paper describes a simple solution to this dilemma: limit the depth of partitioning, and for subproblems that exceed the limit switch to another algorithm with a better worst-case bound. Using heapsort as the "stopper" yields a sorting algorithm that is just as fast as quicksort in the average case but also has an \Theta(N log N) worst case time bound. For selection, a hybrid of Hoare's find algorithm, which is linear on average but quadratic in the worst case, and the Blum-Floyd-Pratt-Rivest-Tarjan algorithm is as fast as Hoare's algorithm in practice, yet has a linear worst-case time bound. Also discussed are issues of implementing the new algorithms as generic algorithms and accurately measuring their performance in the framework of the C++ Standard Template Library.

### Citations

8799 |
Introduction to Algorithms
- Cormen, Leiserson, et al.
- 1992
(Show Context)
Citation Context ...l subproblems to insertion sort is one of the usual optimizations of quicksort; the merits of leaving them until a final pass rather than calling insertion sort immediately are discussed later. As in =-=[13]-=-, block structure is indicated in the pseudo-code using indentation rather than bracketing. Algorithm Quicksort(A, f, b) Inputs: A, a random access data structure containing the sequence of data to be... |

384 | Time Bounds for Selection
- Blum, Floyd, et al.
- 1973
(Show Context)
Citation Context ...f a sequence. Hoare's algorithm [7] based on partitioning has a linear bound in the average case but is quadratic in the worst case. The Blum-Floyd-Pratt-Rivest-Tarjan linear-time worstcase algorithm =-=[8]-=- is much slower on the average than Hoare's algorithm. In this paper, we concentrate on the sorting problem and return to the selection problem only briefly in a later section. The next section presen... |

119 |
STL Tutorial and Reference Guide: C++ Programming with the Standard Template Library, Second Edition
- Musser, Derge, et al.
(Show Context)
Citation Context ...d other operations. These characteristics make median-of-3 quicksort a good candidate for a standard library sorting routine, and it is in fact used as such in the C++ Standard Template Library (STL) =-=[4, 5, 6]-=- as well as in older C libraries---it is the algorithm most commonly used for qsort, for example. However, its worstcase time is \Theta(N 2 ), and although the worst-case behavior appears to be highly... |

119 | The in of caches on the performance of sorting
- LaMarca, Ladner
- 1999
(Show Context)
Citation Context ...sted by Sedgewick [14]. But with modern memory caches the savings in overhead may be cancelled or outweighed for large arrays, since the complete pass at the end can double the number of cache misses =-=[15]-=-. 4 However, in these experiments the many-insertion-sort-call version almost never ran faster than the onefinal -pass version, and in some cases ran considerably slower. One important case is sorting... |

89 |
Implementing quicksort programs
- Sedgewick
- 1978
(Show Context)
Citation Context ...s on small subarrays, rather than at the end of Introsort, where it does one final pass over the entire array. The one-final-pass position is one of the quicksort optimizations suggested by Sedgewick =-=[14]-=-. But with modern memory caches the savings in overhead may be cancelled or outweighed for large arrays, since the complete pass at the end can double the number of cache misses [15]. 4 However, in th... |

62 | Stepanov: Algorithm-oriented Generic Libraries
- Musser, Alexander
- 1994
(Show Context)
Citation Context ...d other operations. These characteristics make median-of-3 quicksort a good candidate for a standard library sorting routine, and it is in fact used as such in the C++ Standard Template Library (STL) =-=[4, 5, 6]-=- as well as in older C libraries---it is the algorithm most commonly used for qsort, for example. However, its worstcase time is \Theta(N 2 ), and although the worst-case behavior appears to be highly... |

59 | Engineering a sort function
- BENTLEY, MCILROY
- 1993
(Show Context)
Citation Context ... as adaptively selecting from larger arrays a larger sample of elements from which to estimate the median. A new version of the C library function qsort based on this technique and other improvements =-=[9]-=- outperforms median-of-3 versions in most cases, but still takes \Theta(N 2 ) time in the worst-case. Yet another possibility is to use a randomized version of quicksort, in which pivot elements are c... |

25 | Average case complexity under the universal distribution equals worst-case complexity
- Li, Vitányi
- 1992
(Show Context)
Citation Context ...h higher probability to sequences that can be produced by short programs, as can the KN sequences, than to random sequences (those that require programs of length proportional to the sequence length) =-=[11]-=-. Under the universal distribution, both quicksort's worst-case and average computing times are \Theta(N 2 ). The average and worst-case times for heapsort---and introsort---remain \Theta(N log N) und... |

24 |
Algorithm 63 (PARTITION) and algorithm 65 (FIND
- Hoare
- 1961
(Show Context)
Citation Context ...to pay a substantial time penalty for the great majority of input sequences. A similar dilemma appears with selection algorithms for finding the i-th smallest element of a sequence. Hoare's algorithm =-=[7]-=- based on partitioning has a linear bound in the average case but is quadratic in the worst case. The Blum-Floyd-Pratt-Rivest-Tarjan linear-time worstcase algorithm [8] is much slower on the average t... |

20 |
personal communication
- Stewart
- 1997
(Show Context)
Citation Context ...s that can cause median-of-3 quicksort to make many bad partitions and take quadratic time. For example, let k be any even positive integer, and consider the following permutation of 1; 2; : : : ; 2k =-=[10]-=-: i 1 2 3 4 5 : : : k \Gamma 2 k \Gamma 1 k k + 1 k + 2 k + 3 : : : 2k \Gamma 1 2k 1 k + 1 3 k + 3 5 : : : 2k \Gamma 3 k \Gamma 1 2k \Gamma 1 2 4 6 : : : 2k \Gamma 2 2k j Call this permutation K 2k ; ... |

4 |
Hewlett-Packard Laboratories reference implementation of the Standard Template Library, source files available from ftp://ftp.cs.rpi.edu/pub/stl
- Stepanov, Lee, et al.
(Show Context)
Citation Context ...lgorithm are perhaps easiest to understand in terms of its differences from the following version of median-of-3 quicksort---the one used in the HP implementation of the C++ Standard Template Library =-=[12]-=- for the sort function. This version uses a constant called size threshold to stop generating subproblems for small sequences, leaving the problem instead for a later pass using insertion sort. Leavin... |

2 |
The Standard Template Library, Technical Report HPL-9434, Hewlett-Packard Laboratories, May 31
- Stepanov, Lee
- 1994
(Show Context)
Citation Context ...d other operations. These characteristics make median-of-3 quicksort a good candidate for a standard library sorting routine, and it is in fact used as such in the C++ Standard Template Library (STL) =-=[4, 5, 6]-=- as well as in older C libraries---it is the algorithm most commonly used for qsort, for example. However, its worstcase time is \Theta(N 2 ), and although the worst-case behavior appears to be highly... |

1 |
Algorithm 132 (heapsort
- Williams
- 1964
(Show Context)
Citation Context ...f-3 quicksort [1, 2] is considered to be a good choice in most contexts. It sorts in place, except for \Theta(log N) stack space, and is usually faster than other in-place algorithms such as heapsort =-=[3]-=-, mainly because it does substantially fewer data assignments and other operations. These characteristics make median-of-3 quicksort a good candidate for a standard library sorting routine, and it is ... |