## On the adaptiveness of quicksort (2005)

### Cached

### Download Links

Venue: | IN: WORKSHOP ON ALGORITHM ENGINEERING & EXPERIMENTS, SIAM |

Citations: | 10 - 1 self |

### BibTeX

@INPROCEEDINGS{Brodal05onthe,

author = {Gerth Stølting Brodal and Rolf Fagerberg and Gabriel Moruz},

title = {On the adaptiveness of quicksort},

booktitle = {IN: WORKSHOP ON ALGORITHM ENGINEERING & EXPERIMENTS, SIAM},

year = {2005},

pages = {130--149},

publisher = {}

}

### OpenURL

### Abstract

Quicksort was first introduced in 1961 by Hoare. Many variants have been developed, the best of which are among the fastest generic sorting algorithms available, as testified by the choice of Quicksort as the default sorting algorithm in most programming libraries. Some sorting algorithms are adaptive, i.e. they have a complexity analysis which is better for inputs which are nearly sorted, according to some specified measure of presortedness. Quicksort is not among these, as it uses Ω(n log n) comparisons even when the input is already sorted. However, in this paper we demonstrate empirically that the actual running time of Quicksort is adaptive with respect to the presortedness measure Inv. Differences close to a factor of two are observed between instances with low and high Inv value. We then show that for the randomized version of Quicksort, the number of element swaps performed is provably adaptive with respect to the measure Inv. More precisely, we prove that randomized Quicksort performs expected O(n(1+log(1+ Inv/n))) element swaps, where Inv denotes the number of inversions in the input sequence. This result provides a theoretical explanation for the observed behavior, and gives new insights on the behavior of the Quicksort algorithm. We also give some empirical results on the adaptive behavior of Heapsort and Mergesort.

### Citations

135 |
The Art of Computer Programming, Volume III: Sorting and Searching
- Knuth
- 1973
(Show Context)
Citation Context ... + log(1 + Inv/n))) time. In the comparison model, this is known to be optimal with respect to the measure Inv [5]. Most classic sorting algorithms, such as Quicksort, Heapsort [6, 23], and Mergesort =-=[11]-=-, are not adaptive: their time complexity is Θ(n log n) irrespectively of the input. However, a large body of adaptive sorting algorithms, such as the one in [13], has been developed over the last thr... |

90 | Implementing quicksort programs - Sedgewick - 1978 |

67 | A survey of adaptive sorting algorithms
- Estivill-Castro, Wood
- 1992
(Show Context)
Citation Context ...include Rem, the minimum number of elements that must be removed for the remaining elements to be sorted, and Runs, the number of consecutive ascending runs. More examples of measures can be found in =-=[5]-=-. An example of an adaptive sorting algorithm is insertion sort using level-linked B-trees with finger searches for locating each new insertion point [13], which sorts in O(n(1 + log(1 + Inv/n))) time... |

63 |
Algorithm 64: Quicksort
- Hoare
- 1961
(Show Context)
Citation Context ...-mail: rolf@imada.sdu.dk. § Supported in part by the Danish Natural Science Research Council (SNF). 1s1 Introduction Quicksort was introduced by Hoare in 1961 as a simple randomized sorting algorithm =-=[8, 9]-=-. Hoare proved that the expected number of comparisons performed by Quicksort for a sequence of n elements is essentially 2n ln n ≈ 1.4n log 2 n [10]. Many variants and analysis of the algorithm have ... |

60 | Engineering a sort function
- Bentley, McIlroy
- 1993
(Show Context)
Citation Context ...cted number of comparisons performed by Quicksort for a sequence of n elements is essentially 2n ln n ≈ 1.4n log 2 n [10]. Many variants and analysis of the algorithm have later been given, including =-=[1, 12, 19, 20, 21]-=-. In practice, tuned versions of Quicksort have turned out to be very competitive, and are used as standard sorting algorithms in many software libraries, e.g. C glibc, C++ STL-library, Java JDK, and ... |

59 | Backwards analysis of randomized geometric algorithms
- Seidel
- 1993
(Show Context)
Citation Context ...heorem 1.1. In Section 3 we describe our experimental setup, and in Section 4 we describe and discuss our experimental results. Parts of our proof of Theorem 1.1 were inspired by the proof by Seidel [=-=Seidel 1992-=-, Section 5] concerning the expected number of comparisons performed by randomized Quicksort. 2. EXPECTED NUMBER OF SWAPS BY RANDOMIZED QUICKSORT In this section we analyze the expected number of elem... |

40 |
Algorithm 245: Treesort3
- Floyd
- 1964
(Show Context)
Citation Context ...], which sorts in O(n(1 + log(1 + Inv/n))) time. In the comparison model, this is known to be optimal with respect to the measure Inv [5]. Most classic sorting algorithms, such as Quicksort, Heapsort =-=[6, 23]-=-, and Mergesort [11], are not adaptive: their time complexity is Θ(n log n) irrespectively of the input. However, a large body of adaptive sorting algorithms, such as the one in [13], has been develop... |

31 |
A high-speed sorting procedure
- Shell
- 1959
(Show Context)
Citation Context ...he question of a theoretical analysis of the number of cache misses of Heapsort as a function of Inv. An interesting sorting algorithm to be considered for study is Shellsort, introduced by Shell in [=-=Shell 1959-=-] and improved over the years (see [Sedgewick 1996] for a comprehensive survey). Since it is based on Insertionsort, we expect Shellsort to outperform some optimal sorting algorithms for a very small ... |

29 | Optimal sampling strategies in quicksort and quickselect
- Martínez, Roura
(Show Context)
Citation Context ...cted number of comparisons performed by Quicksort for a sequence of n elements is essentially 2n ln n ≈ 1.4n log 2 n [10]. Many variants and analysis of the algorithm have later been given, including =-=[1, 12, 19, 20, 21]-=-. In practice, tuned versions of Quicksort have turned out to be very competitive, and are used as standard sorting algorithms in many software libraries, e.g. C glibc, C++ STL-library, Java JDK, and ... |

27 | Analysis of shellsort and related algorithms
- Sedgewick
- 1996
(Show Context)
Citation Context ...umber of cache misses of Heapsort as a function of Inv. An interesting sorting algorithm to be considered for study is Shellsort, introduced by Shell in [Shell 1959] and improved over the years (see [=-=Sedgewick 1996-=-] for a comprehensive survey). Since it is based on Insertionsort, we expect Shellsort to outperform some optimal sorting algorithms for a very small number of inversions, because of a very small numb... |

21 |
A framework for adaptive sorting
- Petersson, Moffat
- 1995
(Show Context)
Citation Context ... algorithms, such as the one in [13], has been developed over the last three decades. For an overview of this area, we refer the reader to the 1992 survey [5]. Later work on adaptive sorting includes =-=[2, 3, 4, 15, 17]-=-. Most of these results are of theoretical nature, and few practical gains in running time have been demonstrated for adaptive sorting algorithms compared to good non-adaptive algorithms. Our starting... |

16 |
Algorithm 63: partition
- Hoare
- 1961
(Show Context)
Citation Context ...-mail: rolf@imada.sdu.dk. § Supported in part by the Danish Natural Science Research Council (SNF). 1s1 Introduction Quicksort was introduced by Hoare in 1961 as a simple randomized sorting algorithm =-=[8, 9]-=-. Hoare proved that the expected number of comparisons performed by Quicksort for a sequence of n elements is essentially 2n ln n ≈ 1.4n log 2 n [10]. Many variants and analysis of the algorithm have ... |

14 |
Sorting and Searching
- Mehlhorn
- 1984
(Show Context)
Citation Context ...uns. More examples of measures can be found in [5]. An example of an adaptive sorting algorithm is insertion sort using level-linked B-trees with finger searches for locating each new insertion point =-=[13]-=-, which sorts in O(n(1 + log(1 + Inv/n))) time. In the comparison model, this is known to be optimal with respect to the measure Inv [5]. Most classic sorting algorithms, such as Quicksort, Heapsort [... |

14 |
Super Scalar Sample Sort
- Sanders, Winkel
- 2004
(Show Context)
Citation Context ...ocedure of the algorithm. For Quicksort and Mergesort we show empirically the strong influence of branch mispredictions on the running time. This is in line with recent findings of Sanders and Winkel =-=[18]-=-, who demonstrate the practical importance of avoiding branch mispredictions in the design of sorting algorithms for current CPU architectures. For Heapsort, our experiments indicate that data cache m... |

13 | Cache-aware and cache-oblivious adaptive sorting - Brodal, Fagerberg, et al. - 2005 |

12 |
Quicksort
- Sedgewick
- 1980
(Show Context)
Citation Context ...uence of n elements is essentially 2n lnn ≈ 1.4n log 2 n [Hoare 1962]. Many variants and analysis of the algorithm have later been given, including [Bentley and McIlroy 1993; Martínez and Roura 2002; =-=Sedgewick 1975-=-; 1977; 1978]. In practice, tuned versions of Quicksort have turned out to be very competitive, and are used as standard sorting algorithms in many software libraries, e.g. C glibc, C++ STL-library, J... |

9 | An In-Place Sorting with O (n log n) Comparisons and O (n) Moves
- Franceschini, Geffert
- 2003
(Show Context)
Citation Context ... sort uses O(n 2 ) comparisons but performs at most n − 1 element swaps for any input. An optimal in-place sorting algorithm performing O(n) swaps and O(n log n) comparisons was recently presented in =-=[7]-=-. This paper is organized as follows: In Section 2 we prove Theorem 1. In Section 3 we describe our experimental setup, and in Section 4 we describe and discuss our experimental results. Parts of our ... |

7 |
Adaptive sorting and the information theoretic lower bound
- Elmasry, Fredman
- 2003
(Show Context)
Citation Context ... algorithms, such as the one in [13], has been developed over the last three decades. For an overview of this area, we refer the reader to the 1992 survey [5]. Later work on adaptive sorting includes =-=[2, 3, 4, 15, 17]-=-. Most of these results are of theoretical nature, and few practical gains in running time have been demonstrated for adaptive sorting algorithms compared to good non-adaptive algorithms. Our starting... |

6 |
Priority queues, pairing, and adaptive sorting
- Elmasry
- 2002
(Show Context)
Citation Context ... algorithms, such as the one in [13], has been developed over the last three decades. For an overview of this area, we refer the reader to the 1992 survey [5]. Later work on adaptive sorting includes =-=[2, 3, 4, 15, 17]-=-. Most of these results are of theoretical nature, and few practical gains in running time have been demonstrated for adaptive sorting algorithms compared to good non-adaptive algorithms. Our starting... |

6 | Sorting and/by merging finger trees
- Moffat, Petersson, et al.
- 1992
(Show Context)
Citation Context ...n which of the two input subsequences provides the next element output. It is easy to verify that the number of such alternations is dominated by the running time of the Mergesort algorithm by Moffat =-=[14]-=- based on merging by finger search trees, which was proved to have a running time of O(n log Inv n ), i.e. the number of alternations by standard Mergesort is O(n log Inv n ). The plots in Figure 12 s... |

5 | On adaptive integer sorting
- Pagh, Pagh, et al.
- 2004
(Show Context)
Citation Context |

3 | Adaptive sorting with AVL trees
- Elmasry
- 2004
(Show Context)
Citation Context |

3 | Tradeoffs between branch mispredictions and comparisons for sorting algorithms - Brodal, Moruz - 2005 |

2 |
Performance Application Programming Interface). Software library found at http://icl.cs.utk.edu/papi
- PAPI
- 2004
(Show Context)
Citation Context ...x 2.4.22. On both machines the C source code was compiled using gcc-3.3.2 with optimization level -O3. The number of branch mispredictions and L2 data cache misses was obtained using the PAPI library =-=[16]-=- version 3.0. Source code and the plotted data are available at ftp://ftp.brics.dk/ RS/04/27/Experiments. 4 Experimental results 4.1 Quicksort. We first analyze the dependence of the version of Quicks... |

1 |
An empirical study for inversions-sensitive sorting algorithms
- Elmasry, Hammad
- 2005
(Show Context)
Citation Context ...h mispredictions, which are an important factor affecting the running time when computation takes place in internal memory. We observe that Mergesort has an adaptive behavior too. Elmasry and Hammad [=-=Elmasry and Hammad 2005-=-] gave an empirical study for optimal algorithms with respect to Inv, and compare these algorithms against Quicksort. For Quicksort they measure the number of comparisons and the running time, obtaini... |