ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
QuicksortAdversary.h File Reference

The adversary of McIlroy against quicksort, beyond the book: a comparator of the items \( 0 , \ldots , n - 1 \) that does not look at their values, since they have none when the sort starts, and gives them their values while the sort runs, so that the quicksort that it is passed to makes \( \Theta( n^2 ) \) comparisons (exercise 11.6 of Morin's book). More...

#include "ADS/Contract.h"
#include <cstddef>
#include <vector>
#include "ADS/sort/impl/QuicksortAdversary.tpp"

Go to the source code of this file.

Classes

class  ADS::QuicksortAdversary
 the adversary of McIlroy: a comparator of \( n \) items that gives them their values while a quicksort runs More...
class  ADS::QuicksortAdversary::Less
 the comparator to pass to a sort, by value: a pointer to the adversary, which must outlive it More...

Namespaces

namespace  ADS

Detailed description

The adversary of McIlroy against quicksort, beyond the book: a comparator of the items \( 0 , \ldots , n - 1 \) that does not look at their values, since they have none when the sort starts, and gives them their values while the sort runs, so that the quicksort that it is passed to makes \( \Theta( n^2 ) \) comparisons (exercise 11.6 of Morin's book).

Every item starts as "gas", larger than every value given; when two gas items are compared, one of them is frozen to the next value, the smallest still free, and the adversary prefers to freeze the item that was last compared with a frozen one, the candidate for the pivot, since a quicksort compares its pivot with all the other items of its part. The pivot is thus frozen at the first comparison of a partition, smaller than all the gas of its part, so that every partition takes away the pivot alone, and a part of \( k \) items costs \( k - 1 \) comparisons or more, \( \binom{n}{2} \) in all. This holds for any quicksort that chooses its pivot among \( O( 1 ) \) items, also by a random draw, also with the partition in three of Morin's book, since the adversary answers after the draw; and since the answers are those of a total order of the values given, the values at the end (the gas left frozen in order) are an input on which the same deterministic sort makes the same comparisons, a killer input for a fixed rule of the pivot, as the first item (exercise 11.3), the middle one (exercise 11.4) or any rule that does not look at the values (exercise 11.5).

The contracts [see Contract.h]: the items compared are below \( n \); input() is a permutation of \( 0 , \ldots , n - 1 \). The operations are defined in QuicksortAdversary.tpp.

See also
M. D. McIlroy, A killer adversary for quicksort, Software: Practice and Experience 29(4), 341-344, 1999, doi:10.1002/(SICI)1097-024X(19990410)29:4<341::AID-SPE237>3.0.CO;2-R
P. Morin, Open Data Structures (in C++), Edition 0.1G, 2013, https://opendatastructures.org/ods-cpp.pdf, section 11.1.2 and exercises 11.3 to 11.6
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa