24

Find the least number of comparisons needed to sort (order) five elements and devise an algorithm that sorts these elements using this number of comparisons.

Solution: There are 5! = 120 possible outcomes. Therefore a binary tree for the sorting procedure will have at least 7 levels. Indeed, $2^h$ ≥ 120 implies $h $ ≥ 7. But 7 comparisons is not enough. The least number of comparisons needed to sort (order) five elements is 8.

Here is my actual question: I did find an algorithm that does it in 8 comparison but how can I prove that it can't be done in 7 comparisons?

PleaseHelp
  • 749
  • 1
  • 8
  • 21

5 Answers5

32

The solution is wrong. Demuth [1; via 2, sec. 5.3.1] shows that five values can be sorted using only seven comparisons, i.e. that the "information theoretic" lower bound is tight in this instance.

The answer is a method tailored to $n=5$, not a general algorithm. It's also not very nice. This is the outline:

  1. Sort the first two pairs.

  2. Order the pairs w.r.t. their respective larger element.

    Call the result $[a,b,c,d,e]$; we know $a<b<d$ and $c<d$.

  3. Insert $e$ into $[a,b,d]$.

  4. Insert $c$ into the result of step 3.

The first step clearly takes two comparisons, the second only one. The last two steps take two comparisons each; we insert into a three-element list in both cases (for step 4., note that we know from $c<d$ that $c$ is smaller than the last element of the list at hand) and compare with the middle element first. That makes a total of seven comparisons.

Since I don't see how to write "nice" pseudocode of this, see here for a tested (and hopefully readable) implementation.


  1. Ph.D. thesis (Stanford University) by H.B. Demuth (1956)

    See also Electronic Data Sorting by H.B. Demuth (1985)

  2. Sorting and Searching by Donald E. Knuth; The Art of Computer Programming Vol. 3 (2nd ed, 1998)
Raphael
  • 73,212
  • 30
  • 182
  • 400
2

The theoretical lower bound on comparison based sorting is $\log(n!)$. That is to say that to sort $n$ items using only $<$ or $>$ comparisons it takes at least the base 2 logarithm of $n!$, hence $\log(5!) \approx 6.91$ operations.

Since $5!= 120$ and $2^7= 128$, using a binary decision tree you can sort 5 items in 7 comparisons. The tree figures out exactly which of the 120 permutations you have, then does the swaps needed to sort it.

It's not pretty or short code, and you should probably use code generation methods to create the decision tree and swaps rather than coding it by hand, but it works; and provably works for any possible permutation of 5 items, thus proving you can sort 5 items in no more than 7 comparisons.

dkaeae
  • 5,057
  • 1
  • 17
  • 31
-1

Some problems are that the question is (1)incompletely stated, and (2)includes unwarranted presumptions:

how can I prove that it can't be done in 7 comparisons?

On the first point, it is unclear what the domain of the sorting problem is: is it random real numbers (with some specified or unspecified distribution), or integers (in some specified or unspecified range), or some other type of objects; are the values guaranteed to be distinct, or could there be duplicates (triplicates, etc.), and so on? If there are duplicates, etc., does it matter whether the partial ordering of items which compare equal is maintained (a.k.a. stable sorting)? None of these things are specified, and they all affect possible (and/or inapplicable) potential solutions to the sorting problem which is behind the stated question. It's also unclear whether the question is about best-case, average-case, or worst-case comparisons (under some specified conditions for some problem domain). And it's unclear whether anything other than comparisons (e.g. amount of data movement, partial-order stability as mentioned above, etc.) is a consideration.

On the second point, one of the responses notes Demuth's 7 comparison solution to non-stable sorting, so the question as stated is an impossible quest; one cannot in fact (correctly) prove the impossibility of something which is in fact possible.

So the short answer to the question as stated is "you can't" (in part because there is a 7 comparison solution to sorting 5 elements with certain constraints (and lacking others), and partly because the question and background do not specify any relevant constraints, details of the problem domain, etc.).

Bruce Lilly
  • 119
  • 1
  • 4
-2

i was thinking quicksort. you select as pivot the element that just happens to be the middle element. compare the pivot to the remaining 4 items resulting in two piles to be sorted. each of those piles can be sorted in 1 comparison. unless i have made a terrible mistake, the 5 items were fully sorted in just 6 comparisons and i think that is the absolute fewest number of comparisons needed to do the job. the original question was find the least number of comparisons to sort 5 elements.

scottyc
  • 1
  • 1
-3

If you can test algorithm, test it on all number combinations. If you have lot of number, test on lot of random combinations. Not precise, but faster than all combinations.

Minimal
a < b < c = 2
a < b < c < d = 3
a < b < c < d < e = 4

Maximal
3^3
4^4
5^5

Insert to middle use 3-6 for 4 numbers.
Merging use 4-5 for 4 numbers.
Minimal compare by wiki is 5 for 4 numbers :) For 5 is 7. You use 8, still so much.
https://en.wikipedia.org/wiki/Comparison_sort#Number_of_comparisons_required_to_sort_a_list
If you know all before comparations, you can go down with comparations. My average for 4 numbers is 3.96 / 1024 all combinations.