# Ordering a list of variables making only direct comparisons - minimum needed?

**URL:** <https://boards.straightdope.com/t/ordering-a-list-of-variables-making-only-direct-comparisons-minimum-needed/744839>\
**Category:** Factual Questions\
**Created:** [February 1, 2016, 12:48pm UTC](https://boards.straightdope.com/t/ordering-a-list-of-variables-making-only-direct-comparisons-minimum-needed/744839 "2016-02-01T12:48:51Z")\
**Posts on this page:** 4\
**Page:** 2

<div class="post-metadata">

**Author:** ![ftg](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/ftg/32/2801_2.png) [@ftg](https://boards.straightdope.com/u/ftg)\
**Post date:** [February 1, 2016, 6:20pm UTC](https://boards.straightdope.com/t/ordering-a-list-of-variables-making-only-direct-comparisons-minimum-needed/744839/21 "2016-02-01T18:20:08Z")

</div>

There is a significant abuse of Big Oh notation going on in this thread. “O” imposes an _upper_ bound on the range only. When answering the OP’s question, you need to use the _lower_ bound notation, “[Big Omega](https://en.wikipedia.org/wiki/Big_O_notation#Big_Omega_notation)”: Ω.

I.e., the information theory _lower_ bound on sorting by comparison is Ω\*(n log n)\*. The _upper_ bound _O(n log n)_ is proven by the existence of algorithms such as Mergesort.

One thing I like about these comparison-bound arguments is the following: Finding either the min or max element takes _n_-1 comparisons (all but one element has to “lose” a comparison). But to find both the min and max takes ⌈3_n_/2⌉-2 comparisons (upper and lower bound). Somewhat of an improvement over the naive 2_n_-2 method. (For seem reason this seems to be a common job interview question for programmers.)

While non-trivial lower bounds are rare in Algorithm Analysis, the restriction to comparisons only helps in cases such as this.

---

<div class="post-metadata">

**Author:** ![septimus](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/septimus/32/410_2.png) [@septimus](https://boards.straightdope.com/u/septimus)\
**Post date:** [February 2, 2016, 12:35am UTC](https://boards.straightdope.com/t/ordering-a-list-of-variables-making-only-direct-comparisons-minimum-needed/744839/22 "2016-02-02T00:35:41Z")

</div>

I made a little effort to track down the paper I mentioned about Quicksort and its worst cases. It might have been one of these

[http://www.cs.dartmouth.edu/~doug/mdmspe.pdf](http://www.cs.dartmouth.edu/~doug/mdmspe.pdf)

> **[bentley93engineering.pdf](https://cs.fit.edu/~pkc/classes/writing/samples/bentley93engineering.pdf)**
>
> 99.29 KB

> [@](#):
>
> qsort … based on Scowen’s Quickersort had served faithfully [for] almost two decades… Yet in the summer of 1991 our colleagues Allan Wilks and Rick Becker found that a qsort run that should have taken a few minutes was chewing up hours of CPU time. Had they not interrupted it, it would have gone on for weeks. They found that it took n^2 comparisons to sort an ‘organ-pipe’ array of 2n integers: 123…nn… 321.

---

<div class="post-metadata">

**Author:** ![Chronos](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/chronos/32/134_2.png) [@Chronos](https://boards.straightdope.com/u/Chronos)\
**Post date:** [February 2, 2016, 2:07am UTC](https://boards.straightdope.com/t/ordering-a-list-of-variables-making-only-direct-comparisons-minimum-needed/744839/23 "2016-02-02T02:07:27Z")

</div>

> [@](#):
>
> Quoth **ftg** :
> 
> There is a significant abuse of Big Oh notation going on in this thread.

I invoke the Physicists’ Bill of Rights, specifically the right to abuse notation whenever convenient. What’s you guys’ excuse?

---

<div class="post-metadata">

**Author:** ![LSLGuy](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/lslguy/32/5813_2.png) [@LSLGuy](https://boards.straightdope.com/u/LSLGuy)\
**Post date:** [February 2, 2016, 2:11am UTC](https://boards.straightdope.com/t/ordering-a-list-of-variables-making-only-direct-comparisons-minimum-needed/744839/24 "2016-02-02T02:11:31Z")

</div>

@ **septimus** :

Thank you. Good stuff. The first paper was particularly evil, designing a subversive function to compute & feed pathological data to the target sort. Obviously as an attack vector that depends on being able to inject your machine code into the execution stream.

Both papers describe the generic properties of pathological data. So that knowledge could be used offline without needing to code inject.

[Previous page](https://boards.straightdope.com/t/ordering-a-list-of-variables-making-only-direct-comparisons-minimum-needed/744839.md?page=1)
