A killer adversary for quicksort

M. D. McIlroy
<span title="1999-04-10">1999</span> <i title="Wiley"> <a target="_blank" rel="noopener" href="https://fatcat.wiki/container/afve5b3tavbfzch46mdtazbqla" style="color: black;">Software, Practice &amp; Experience</a> </i> &nbsp;
Quicksort can be made to go quadratic by constructing input on the fly in response to the sequence of items compared. The technique is illustrated by a specific adversary for the standard C qsort function. The general method works against any implementation of quicksort-even a randomizing one-that satisfies certain very mild and realistic assumptions. When using quicksort one often feels a nagging tension: suppose it goes quadratic? Tactics to avoid embarrassing results in some low-entropy
more &raquo; ... , such as already ordered input, are cited in most algorithms books 12 and are widely used. Nevertheless, production implementations have been caught going quadratic in real-life applications. 3 No matter how hard implementers try, they cannot (without great sacrifice of speed) defend against all inputs. This note describes an adversarial method that finds chinks in the defenses of any implementation. A polymorphic implementation of quicksort, such as the standard C function qsort, never looks at the data. It relies instead on an externally supplied comparison function. And that allows us to monitor and influence the program's progress noninvasively. To do so we make a comparison function that observes the pattern of comparisons and constructs adverse data on the fly. Recall that quicksort sorts a sequence of n data items in three phases:
<span class="external-identifiers"> <a target="_blank" rel="external noopener noreferrer" href="https://doi.org/10.1002/(sici)1097-024x(19990410)29:4&lt;341::aid-spe237&gt;3.0.co;2-r">doi:10.1002/(sici)1097-024x(19990410)29:4&lt;341::aid-spe237&gt;3.0.co;2-r</a> <a target="_blank" rel="external noopener" href="https://fatcat.wiki/release/z5qpcvffkrel5mgw3zeny2zwti">fatcat:z5qpcvffkrel5mgw3zeny2zwti</a> </span>
<a target="_blank" rel="noopener" href="https://web.archive.org/web/20081019190204/http://www.cs.auckland.ac.nz/%7Emcw/Teaching/refs/sorting/quicksort-killer.pdf" title="fulltext PDF download" data-goatcounter-click="serp-fulltext" data-goatcounter-title="serp-fulltext"> <button class="ui simple right pointing dropdown compact black labeled icon button serp-button"> <i class="icon ia-icon"></i> Web Archive [PDF] <div class="menu fulltext-thumbnail"> <img src="https://blobs.fatcat.wiki/thumbnail/pdf/dd/00/dd006b6383bd512c7645d8408fa6c97875c27318.180px.jpg" alt="fulltext thumbnail" loading="lazy"> </div> </button> </a> <a target="_blank" rel="external noopener noreferrer" href="https://doi.org/10.1002/(sici)1097-024x(19990410)29:4&lt;341::aid-spe237&gt;3.0.co;2-r"> <button class="ui left aligned compact blue labeled icon button serp-button"> <i class="external alternate icon"></i> wiley.com </button> </a>