in Algorithms edited by
3,510 views
9 votes
9 votes
The lower bound on the no. of comparisons required to sort n numbers is __________ ?
in Algorithms edited by
by
3.5k views

2 Answers

15 votes
15 votes
Best answer


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

4 Comments

in ceil (log 120) what will be the base of the log?
0
0

@Divyanshum29 

It's 2.

Refer the wiki link provided in the answer

1
1
But on applying base as 2, the answer is not equal to 30. Why is it so?
0
0
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
by

4 Comments

Then use shuttle ..i use both.. little handicapped software, but have to deal with that.
2
2
Nice explanation! just increased my fondness in beauty of mathematics.
1
1
Thanks! That was the intent :)
0
0

Related questions