Presentation is loading. Please wait.

Presentation is loading. Please wait.

Instructor Neelima Gupta Table of Contents Review of Lower Bounding Techniques Decision Trees Linear Sorting Selection Problems.

Similar presentations


Presentation on theme: "Instructor Neelima Gupta Table of Contents Review of Lower Bounding Techniques Decision Trees Linear Sorting Selection Problems."— Presentation transcript:

1 Instructor Neelima Gupta ngupta@cs.du.ac.in

2 Table of Contents Review of Lower Bounding Techniques Decision Trees Linear Sorting Selection Problems

3 Lower Bounds Any algorithm that sorts by removing at most one inversion per comparison does at least n(n-1)/2 comparisons in the worst case. Any comparison based sorting algorithm must do at least  (n lg n) comparisons.

4 Optimal algorithms Insertion Sort, Selection Sort, Bubble Sort are optimal in the first category of algorithms. Merge Sort and Heap Sort are optimal in the second category of algorithms. Is Qsort optimal in the first or second category?

5 Lower Bound For Comparison Sorting Thm: Any decision tree that sorts n elements has height  (n lg n) What’s the minimum # of leaves? What’s the maximum # of leaves of a binary tree of height h? Clearly the minimum # of leaves is less than or equal to the maximum # of leaves

6 Lower Bound For Comparison Sorting So we have… n!  2 h Taking logarithms: lg (n!)  h Stirling’s approximation tells us: Thus:

7 Lower Bound For Comparison Sorting So we have Thus the minimum height of a decision tree is  (n lg n)

8 Sorting in Linear Time Count Sort Radix Sort Limitations of these algorithms What about bucket sort? – Is it linear in the worst case? – When is it linear?

9 Selection Problems Select ith rank element Min-Max Problem Largest and Second largest

10 Algorithm1 to find Min-Max in 2n-3 comparisons STEPS: 1.All keys are assumed to be distinct. 2.Maximum of n keys can be found in n-1 comparisons. 3.Eliminating max key we are left with n-1 keys. 4. Minimum of n-1 keys can be found in n-2 comparisons. 5.Total no of comparisons are (n-1)+(n-2)=2n-3.

11 Algorithm2 to find Min-Max in 3n/2 - 2 comparisons STEPS: 1.All keys are assumed to be distinct. 2.We perform comparison on n/2 pairs. 3.After n/2 comparisons we get n/2 winners and n/2 loosers. 4.So MAX of n keys is the maximum of these n/2 winners. 5.Similarly MIN of n keys is the minimum of these n/2 loosers.

12 Finding the Largest n the Second Largest If an algorithm first finds largest in (n-1) comparisons and then finds 2 nd largest from the remaining (n-1) elements in (n-2) comparisons, then Total no. of comaprisons to find 2 nd largest = ( n-1 ) + ( n-2 ) = 2n – 3 Next, we’ll see an algorithm which does better than this.

13 Tournament Method Consider the method to find largest. For e.g 5 2 8 6 9 3 1 4 5 894 89 9 largest

14 In this tree, we have 8-1 = 7 comparisons. As 8, 3 & 4 loose only once to reach largest, they are the only candidates for 2 nd largest. no. of candidates for 2 nd largest = height of the tree = log n Note:- If no. of keys is odd then extra key is considered in the end.

15 As no.of keys = lg n (for 2 nd largest ) therefore, no. of comparisons to find 2 nd largest = lg n -1. Thus, total comparisons = ( n-1) + ( lg n -1) = n + lgn – 2

16 Up Next Lower Bounds for Selection Problem


Download ppt "Instructor Neelima Gupta Table of Contents Review of Lower Bounding Techniques Decision Trees Linear Sorting Selection Problems."

Similar presentations


Ads by Google