Sorting Lower Bounds Amihood Amir Bar-Ilan University 2014
Sorting Algorithms We have seen: Can we sort faster? AlgorithmTimeIn Place Bubblesort O(n2)O(n2) yes Insertion sort O(n2)O(n2) yes Selection sort O(n2)O(n2) yes Merge sort O(n log n) no Heap sort O(n log n) yes
Comparison Based Sorting Assume a general ordered set. The only logical operation used is comparing elements. Example: All sorting algorithms in the table above.
Comparison Tree If we “clean up” all operations and just leave a flow chart of the comparisons, a run of the algorithm is a tree where every node is a comparison of the type A<B
BubbleSort Example END A[1]<A[2]? A[2]<A[3]? A[1]<A[2]? YES NO: BUBBLE! The Comparison Tree of The Bobblesort 3 element algorithm: C,B,A C,A,B B,A,C B,C,AA,C,B A,B,C
Comparison Tree Properties Comparison tree for sorting n elements: Number of leaves: n! Algorithm Running Time: height of tree. Best possible Time: height of tree = log n! What is log n! ?
Comparison Sorting Lower Bound n/2· n/2· · ·n/2 < 1 ·2 ·3 · · ·n < n ·n ·n · · ·n (n/2) n/2 < n! < n n n/2 log (n/2) < log n! < n log n n/2 (log n – 1) < log n! < n log n Conclude: log n! is O(n log n) · n/2 timesn times
Optimal Sorting Time No comparison-based sorting algorithm can be faster than O(n log n) in the worst case. Conclude: Heapsort is an optimal time in-place algorithm