# Lower bound for comparison based sorting algorithms

The problem of sorting can be viewed as following.

**Input:** A sequence of *n* numbers <*a*_{1}, *a*_{2}, . . . , *a _{n}*>.

**Output:**A permutation (reordering) <

*a*‘

_{1},

*a*‘

_{2}, . . . ,

*a*‘

*> of the input sequence such that*

_{n}*a*‘

_{1}<=

*a*‘

_{2}….. <= a‘

*.*

_{n}A sorting algorithm is comparison based if it uses comparison operators to find the order between two numbers. Comparison sorts can be viewed abstractly in terms of decision trees. A decision tree is a full binary tree that represents the comparisons between elements that are performed by a particular sorting algorithm operating on an input of a given size. The execution of the sorting algorithm corresponds to tracing a path from the root of the decision tree to a leaf. At each internal node, a comparison *a _{i}* <=

*a*is made. The left subtree then dictates subsequent comparisons for

_{j}*a*<=

_{i}*a*, and the right subtree dictates subsequent comparisons for

_{j}*a*>

_{i}*a*. When we come to a leaf, the sorting algorithm has established the ordering. So we can say following about the decison tree.

_{j}**2) **Let x be the maximum number of comparisons in a sorting algorithm. The maximum height of the decison tree would be x. A tree with maximum height x has at most 2^x leaves.

After combining the above two facts, we get following relation.

n! <= 2^x Taking Log on both sides. log_{2}(n!) <= x Since log_{2}(n!) = Θ(nLogn), we can say x = Ω(nLog_{2}n)

Therefore, any comparison based sorting algorithm must make at least nLog_{2}n comparisons to sort the input array, and Heapsort and merge sort are asymptotically optimal comparison sorts.

**References:**

Introduction to Algorithms, by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest and Clifford Stein

## Recommended Posts:

- Asymptotic Analysis and comparison of sorting algorithms
- Sorting without comparison of elements
- Stability in sorting algorithms
- Time Complexities of all Sorting Algorithms
- Sorting Algorithms Visualization : Bubble Sort
- Loop Invariant Condition with Examples of Sorting Algorithms
- Know Your Sorting Algorithm | Set 1 (Sorting Weapons used by Programming Languages)
- Know Your Sorting Algorithm | Set 2 (Introsort- C++’s Sorting Weapon)
- Alternate Lower Upper String Sort
- Sort the character array based on ASCII % N
- External Sorting
- Pancake sorting
- Sorting in Java
- Sorting Big Integers
- Sorting Terminology