edited by
3,454 views

2 Answers

Best answer
15 votes
15 votes


http://en.wikipedia.org/wiki/Comparison_sort#Number_of_comparisons_required_to_sort_a_list


Hence, upto $N=12$, It is $CEIL(LogN!)$ and after that, it is $O(NLogN)$.

Thank you @Kapil : ) 

edited by
29 votes
29 votes

                                         $$\large\color{green}{\text{Minimum comparison sorting:}}$$

First of all, we will not consider any existing comparison based sorting algorithm, and try to see this problem from a basic point of view of comparison.

Now,
Assume our keys to be distinct. And keys are 

$$\begin{align*} \color{red}{K_1 \;\;K_2 \;\;K_3 \;\;K_4 \;\; ..... K_n } \end{align*}$$

  • Because we are considering distinct keys there are only two possibilities of comparing two keys $K_i$ and $K_j$.
  • Either $K_i > K_j$ or, $K_i < K_j$


This problem of sorting by comparison can also be visualized as follows:

Given a set of $n$ distinct weights and a balance scale, we can ask for the least no of weighings necessary to completely rank the weights in the order of magnitude. The only constraint is we can measure only two weights in a single weighing.

Now we abstract our problem with the following extended binary tree. For this example only 3 keys are taken.

  • Each internal node of this tree has two indices $i:j$, denoting comparison is in between $K_i$ and $K_j$.
  • Left subtree of this node represents the subsequent comparisons to be made if $K_i < K_j$.
  • Right subtree of this node represents the subsequent comparisons to be made if $K_i > K_j$.
  • Each leaf node of the tree contains a permutation of all ${K_i}$.
  • Each of those permutations is in a sorted order.


One path trace explanation in the above tree (blue path):


 

  • First compare $K_1$ with $K_2$
  • If $K_1 < K_2$ it goes (via left subtree) on to compare $K_2$ with $K_3$
  • If $K_2 > K_3$ it goes (via right subtree) on to compare $K_1$ with $K_3$
  • If $K_1 > K_3$ we arrive at the leaf node with a sorted sequence of $K_3 <  K_1 < K_2$


Important to note here, is that our priority should be to minimize the no of comparison, and if we see in the tree diagram there is no redundant comparison.


For example, after the violet node, in the left subtree we do not need to compare $K_1$ with $K_3$.

So, now we have an extended binary tree structure in which every leaf node corresponds to a unique permutation of the n keys that we have taken in the beginning.

  • We have $\large\color{maroon}{\text{n!}}$ leaf nodes.

Now we are in a position to analyze the minimum no of comparison to sort $n$ given keys with the help of this comparison tree diagram.

Let $C(n)$ be the minimum no of comparisons that that is sufficient to sort $n$ elements.

Few last observation we need.

  • If we are at a level $k$ in the tree, at that level maximum $2^{k}$ nodes can be present. We understand that all the level will not have always $2^{k}$ nodes (sometimes less). But at max, we can assume that.
  • We start from level $0$ (from the root), then arrive at level $k$, we will be done with k comparisons.
  • This one is important: If all the internal nodes at levels $<$ k, then there can be at most $2^{k}$ leaf nodes. Beyond this k levels, we have only leaf nodes which are nothing but required sorted sequence. Thus we performed k comparisons. So, $C(n) = k$.

$$\begin{align*}
&\Rightarrow  n! \leq 2^{C(n)} \\
&\Rightarrow \left \lceil \log (n!) \right \rceil \leq C(n) \\
&\Rightarrow C(n) \geq  \left \lceil \log (n!) \right \rceil   \\
\\
& \text{Using Stirlings approximation} \\
&\Rightarrow C(n) \geq  \;\; n \log n - \frac{n}{\ln 2} + \frac{1}{2} \log n + O(1) \\
&\Rightarrow C(n) \approx  \;\; n \log n \\
\end{align*}$$



For more on information theoretic lower bound :: knuth volume 3 page 180+.

edited by

Related questions

1 votes
1 votes
1 answer
1
iarnav asked May 4, 2019
834 views
I’ve seen this wikipedia article – https://en.wikipedia.org/wiki/Comparison_sortAlso see this link – https://gateoverflow.in/32948/minimum-number-of-comparisonshttp...
1 votes
1 votes
3 answers
2
Angkit asked Apr 23, 2017
14,417 views
Which sorting algorithm can be used to sort a random linked list with minimum time complexity ?A)mergesortB)quicksortC)radixsortD)insertionsortE)heapsort
0 votes
0 votes
1 answer
4
soorajchn asked Mar 14, 2015
5,453 views
a) n- ( lg(n)) - 2b) n + (lg(n)-2)