Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sorting Lower Bounds Amihood Amir Bar-Ilan University 2014.

Similar presentations


Presentation on theme: "Sorting Lower Bounds Amihood Amir Bar-Ilan University 2014."— Presentation transcript:

1 Sorting Lower Bounds Amihood Amir Bar-Ilan University 2014

2 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

3 Comparison Based Sorting Assume a general ordered set. The only logical operation used is comparing elements. Example: All sorting algorithms in the table above.

4 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

5 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

6 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! ?

7 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

8 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


Download ppt "Sorting Lower Bounds Amihood Amir Bar-Ilan University 2014."

Similar presentations


Ads by Google