Presentation is loading. Please wait.

Presentation is loading. Please wait.

David Kauchak cs302 Spring 2012

Similar presentations


Presentation on theme: "David Kauchak cs302 Spring 2012"— Presentation transcript:

1 David Kauchak cs302 Spring 2012
Sorting Conclusions David Kauchak cs302 Spring 2012

2 Administrative Assignment 5 out today

3 Sorting bounds Mergsort is O(n log n)
Quicksort is O(n log n) on average Can we do better?

4 Comparison-based sorting
Sorted order is determined based only on a comparison between input elements A[i] < A[j] A[i] > A[j] A[i] = A[j] A[i] ≤ A[j] A[i] ≥ A[j] Do any of the sorting algorithms we’ve looked at use additional information?

5 Comparison-based sorting
Sorted order is determined based only on a comparison between input elements A[i] < A[j] A[i] > A[j] A[i] = A[j] A[i] ≤ A[j] A[i] ≥ A[j] Do any of the sorting algorithms we’ve looked at use additional information? No All the algorithms we’ve seen are comparison-based sorting algorithms

6 Comparison-based sorting
Sorted order is determined based only on a comparison between input elements A[i] < A[j] A[i] > A[j] A[i] = A[j] A[i] ≤ A[j] A[i] ≥ A[j] In Java (and many languages) for a class of objects to be sorted we define a comparator What does it do?

7 Comparison-based sorting
Sorted order is determined based only on a comparison between input elements A[i] < A[j] A[i] > A[j] A[i] = A[j] A[i] ≤ A[j] A[i] ≥ A[j] In Java (and many languages) for a class of objects to be sorted we define a comparator What does it do? Just compares any two elements Useful for comparison-based sorting algorithms

8 Comparison-based sorting
Sorted order is determined based only on a comparison between input elements A[i] < A[j] A[i] > A[j] A[i] = A[j] A[i] ≤ A[j] A[i] ≥ A[j] Can we do better than O(n log n) for comparison based sorting approaches?

9 Decision-tree model Full binary tree representing the comparisons between elements by a sorting algorithm Internal nodes contain indices to be compared Leaves contain a complete permutation of the input Tracing a path from root to leave gives the correct reordering/permutation of the input for an input 1:3 > [3, 12, 7] | 1,3,2 | [3, 7, 12] | 1,3,2 | [7, 3, 12] | 2,1,3 | [3, 7, 12]

10 A decision tree model 1:2 ≤ > 1:3 2:3 ≤ > ≤ > |2,1,3| 1:3 2:3
|1,2,3| > > |1,3,2| |3,1,2| |2,3,1| |3,2,1|

11 A decision tree model [12, 7, 3] 1:2 ≤ > 1:3 2:3 ≤ > ≤ >
|2,1,3| 1:3 2:3 |1,2,3| > > |1,3,2| |3,1,2| |2,3,1| |3,2,1| [12, 7, 3]

12 A decision tree model [12, 7, 3] Is 12 ≤ 7 or is 12 > 7? 1:2 ≤ >
1:3 2:3 > > |2,1,3| 1:3 2:3 |1,2,3| > > |1,3,2| |3,1,2| |2,3,1| |3,2,1| [12, 7, 3] Is 12 ≤ 7 or is 12 > 7?

13 A decision tree model [12, 7, 3] Is 12 ≤ 3 or is 12 > 3? 1:2 ≤ >
1:3 2:3 > > |2,1,3| 1:3 2:3 |1,2,3| > > |1,3,2| |3,1,2| |2,3,1| |3,2,1| [12, 7, 3] Is 12 ≤ 3 or is 12 > 3?

14 A decision tree model [12, 7, 3] Is 12 ≤ 3 or is 12 > 3? 1:2 ≤ >
1:3 2:3 > > |2,1,3| 1:3 2:3 |1,2,3| > > |1,3,2| |3,1,2| |2,3,1| |3,2,1| [12, 7, 3] Is 12 ≤ 3 or is 12 > 3?

15 A decision tree model [12, 7, 3] Is 12 ≤ 3 or is 12 > 3? 1:2 ≤ >
1:3 2:3 > > |2,1,3| 1:3 2:3 |1,2,3| > > |1,3,2| |3,1,2| |2,3,1| |3,2,1| [12, 7, 3] Is 12 ≤ 3 or is 12 > 3?

16 A decision tree model [12, 7, 3] Is 7 ≤ 3 or is 7 > 3? 1:2 ≤ >
1:3 2:3 > > |2,1,3| 1:3 2:3 |1,2,3| > > |1,3,2| |3,1,2| |2,3,1| |3,2,1| [12, 7, 3] Is 7 ≤ 3 or is 7 > 3?

17 A decision tree model [12, 7, 3] Is 7 ≤ 3 or is 7 > 3? 1:2 ≤ >
1:3 2:3 > > |2,1,3| 1:3 2:3 |1,2,3| > > |1,3,2| |3,1,2| |2,3,1| |3,2,1| [12, 7, 3] Is 7 ≤ 3 or is 7 > 3?

18 A decision tree model 3, 2, 1 [12, 7, 3] 1:2 ≤ > 1:3 2:3 ≤ > ≤
|2,1,3| 1:3 2:3 |1,2,3| > > |1,3,2| |3,1,2| |2,3,1| |3,2,1| 3, 2, 1 [12, 7, 3]

19 A decision tree model 3, 2, 1 [3, 7, 12] [12, 7, 3] 1:2 ≤ > 1:3 2:3
|2,1,3| 1:3 2:3 |1,2,3| > > |1,3,2| |3,1,2| |2,3,1| |3,2,1| 3, 2, 1 [3, 7, 12] [12, 7, 3]

20 A decision tree model [7, 12, 3] 1:2 ≤ > 1:3 2:3 ≤ > ≤ >
|2,1,3| 1:3 2:3 |1,2,3| > > |1,3,2| |3,1,2| |2,3,1| |3,2,1| [7, 12, 3]

21 A decision tree model [7, 12, 3] 1:2 ≤ > 1:3 2:3 ≤ > ≤ >
|2,1,3| 1:3 2:3 |1,2,3| > > |1,3,2| |3,1,2| |2,3,1| |3,2,1| [7, 12, 3]

22 A decision tree model [7, 12, 3] 1:2 ≤ > 1:3 2:3 ≤ > ≤ >
|2,1,3| 1:3 2:3 |1,2,3| > > |1,3,2| |3,1,2| |2,3,1| |3,2,1| [7, 12, 3]

23 A decision tree model [7, 12, 3] 1:2 ≤ > 1:3 2:3 ≤ > ≤ >
|2,1,3| 1:3 2:3 |1,2,3| > > |1,3,2| |3,1,2| |2,3,1| |3,2,1| [7, 12, 3]

24 A decision tree model [7, 12, 3] 1:2 ≤ > 1:3 2:3 ≤ > ≤ >
|2,1,3| 1:3 2:3 |1,2,3| > > |1,3,2| |3,1,2| |2,3,1| |3,2,1| [7, 12, 3]

25 A decision tree model [3, 7, 12] [7, 12, 3] 1:2 ≤ > 1:3 2:3 ≤ >
|2,1,3| 1:3 2:3 |1,2,3| > > |1,3,2| |3,1,2| |2,3,1| |3,2,1| [3, 7, 12] [7, 12, 3]

26 How many leaves are in a decision tree?
Leaves must have all possible permutations of the input What if decision tree model didn’t? Input of size n, n! leaves Some input would exist that didn’t have a correct reordering

27 A lower bound What is the worst-case number of comparisons for a tree?
1:2 > 1:3 2:3 > > |2,1,3| 1:3 2:3 |1,2,3| > > |1,3,2| |3,1,2| |2,3,1| |3,2,1|

28 A lower bound The longest path in the tree, i.e. the height 1:2 ≤ >
1:3 2:3 > > |2,1,3| 1:3 2:3 |1,2,3| > > |1,3,2| |3,1,2| |2,3,1| |3,2,1|

29 A lower bound What is the maximum number of leaves a binary tree of height h can have? A complete binary tree has 2h leaves log is monotonically increasing from hw 

30 Can we do better?


Download ppt "David Kauchak cs302 Spring 2012"

Similar presentations


Ads by Google