6

I've been thinking about sorting $6$ elements with the minimal possible number of comparisons. I can do it in $10$ comparisons but I've no idea if this is optimal. Or is there a better algorithm ?

Algorithm
1. Sort $a_1, a_2, a_3$ and $a_4, a_5, a_6$.
Number of comparisons: $3+3=6$.
2. Merge two subarrays.
Number of comparisons: $3 + 3 - 2 = 4$.
Total number of comparisons: $6 + 4 = 10$.

Raphael
  • 73,212
  • 30
  • 182
  • 400
user40545
  • 583
  • 6
  • 19

2 Answers2

4

TL-DR; $10$

Yes, you need to have possibility to ask up to $10$ questions to (comparison) sort an array of $6$ elements.

However, it is possible to create an optimal algorithm that will require no more than 10 questions in worst case, but in average only $9.57777$ questions (in $416$ cases $10$ comparisons, in $304$ cases only $9$).

Here is a decision tree for sorting 6 element arrays [a,b,c,d,e,f]. To make it more compressed. I assumed that already $a<b$ , $c<d$ and $e<f$ has been asked (3 comparisons) and in case of false proper pair has been swapped: Compressed DT for sorting 6 elements As you can see, in addition to these 3 comparisons, we need up to 7 more, but often only 6 that gives us the $9.57777$ in average.

1

According to A036604, 10 comparisons are indeed optimal. The link probably contains a citation to a paper proving this.

Yuval Filmus
  • 280,205
  • 27
  • 317
  • 514