lower bound on sorting
DESCRIPTION
Lower Bound on Sorting. David Kauchak cs062 Spring 2010. Decision-tree model. B inary 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 - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Lower Bound on Sorting](https://reader033.vdocuments.site/reader033/viewer/2022052603/56813056550346895d960cf1/html5/thumbnails/1.jpg)
Lower Bound on Sorting
David Kauchak
cs062
Spring 2010
![Page 2: Lower Bound on Sorting](https://reader033.vdocuments.site/reader033/viewer/2022052603/56813056550346895d960cf1/html5/thumbnails/2.jpg)
Decision-tree model 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
| 1,3,2 |
≤ >
| 1,3,2 |
| 2,1,3 |
[3, 12, 7]
[7, 3, 12]
[3, 7, 12]
[3, 7, 12]
![Page 3: Lower Bound on Sorting](https://reader033.vdocuments.site/reader033/viewer/2022052603/56813056550346895d960cf1/html5/thumbnails/3.jpg)
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]
This is why most built-in sorting approaches only require you to define the comparison operator (i.e. compareTo in Java)
Can we do better than O(n log n)?
![Page 4: Lower Bound on Sorting](https://reader033.vdocuments.site/reader033/viewer/2022052603/56813056550346895d960cf1/html5/thumbnails/4.jpg)
A decision tree model
1:2≤ >
2:3≤ >
1:3≤ >
|1,2,3|1:3
≤ >2:3
≤ >
|1,3,2| |3,1,2| |2,3,1| |3,2,1|
|2,1,3|
![Page 5: Lower Bound on Sorting](https://reader033.vdocuments.site/reader033/viewer/2022052603/56813056550346895d960cf1/html5/thumbnails/5.jpg)
A decision tree model
1:2≤ >
2:3≤ >
1:3≤ >
|1,2,3|1:3
≤ >2:3
≤ >
|1,3,2| |3,1,2| |2,3,1| |3,2,1|
|2,1,3|
[12, 7, 3]
![Page 6: Lower Bound on Sorting](https://reader033.vdocuments.site/reader033/viewer/2022052603/56813056550346895d960cf1/html5/thumbnails/6.jpg)
A decision tree model
1:2≤ >
2:3≤ >
1:3≤ >
|1,2,3|1:3
≤ >2:3
≤ >
|1,3,2| |3,1,2| |2,3,1| |3,2,1|
|2,1,3|
[12, 7, 3]
Is 12 ≤ 7 or is 12 > 7?
![Page 7: Lower Bound on Sorting](https://reader033.vdocuments.site/reader033/viewer/2022052603/56813056550346895d960cf1/html5/thumbnails/7.jpg)
A decision tree model
1:2≤ >
2:3≤ >
1:3≤ >
|1,2,3|1:3
≤ >2:3
≤ >
|1,3,2| |3,1,2| |2,3,1| |3,2,1|
|2,1,3|
[12, 7, 3]
Is 12 ≤ 3 or is 12 > 3?
![Page 8: Lower Bound on Sorting](https://reader033.vdocuments.site/reader033/viewer/2022052603/56813056550346895d960cf1/html5/thumbnails/8.jpg)
A decision tree model
1:2≤ >
2:3≤ >
1:3≤ >
|1,2,3|1:3
≤ >2:3
≤ >
|1,3,2| |3,1,2| |2,3,1| |3,2,1|
|2,1,3|
[12, 7, 3]
Is 12 ≤ 3 or is 12 > 3?
![Page 9: Lower Bound on Sorting](https://reader033.vdocuments.site/reader033/viewer/2022052603/56813056550346895d960cf1/html5/thumbnails/9.jpg)
A decision tree model
1:2≤ >
2:3≤ >
1:3≤ >
|1,2,3|1:3
≤ >2:3
≤ >
|1,3,2| |3,1,2| |2,3,1| |3,2,1|
|2,1,3|
[12, 7, 3]
Is 7 ≤ 3 or is 7 > 3?
![Page 10: Lower Bound on Sorting](https://reader033.vdocuments.site/reader033/viewer/2022052603/56813056550346895d960cf1/html5/thumbnails/10.jpg)
A decision tree model
1:2≤ >
2:3≤ >
1:3≤ >
|1,2,3|1:3
≤ >2:3
≤ >
|1,3,2| |3,1,2| |2,3,1| |3,2,1|
|2,1,3|
[12, 7, 3]
Is 7 ≤ 3 or is 7 > 3?
![Page 11: Lower Bound on Sorting](https://reader033.vdocuments.site/reader033/viewer/2022052603/56813056550346895d960cf1/html5/thumbnails/11.jpg)
A decision tree model
1:2≤ >
2:3≤ >
1:3≤ >
|1,2,3|1:3
≤ >2:3
≤ >
|1,3,2| |3,1,2| |2,3,1| |3,2,1|
|2,1,3|
[12, 7, 3]3, 2, 1
![Page 12: Lower Bound on Sorting](https://reader033.vdocuments.site/reader033/viewer/2022052603/56813056550346895d960cf1/html5/thumbnails/12.jpg)
A decision tree model
1:2≤ >
2:3≤ >
1:3≤ >
|1,2,3|1:3
≤ >2:3
≤ >
|1,3,2| |3,1,2| |2,3,1| |3,2,1|
|2,1,3|
[12, 7, 3]3, 2, 1
[3, 7, 12]
![Page 13: Lower Bound on Sorting](https://reader033.vdocuments.site/reader033/viewer/2022052603/56813056550346895d960cf1/html5/thumbnails/13.jpg)
A decision tree model
1:2≤ >
2:3≤ >
1:3≤ >
|1,2,3|1:3
≤ >2:3
≤ >
|1,3,2| |3,1,2| |2,3,1| |3,2,1|
|2,1,3|
[7, 12, 3]
![Page 14: Lower Bound on Sorting](https://reader033.vdocuments.site/reader033/viewer/2022052603/56813056550346895d960cf1/html5/thumbnails/14.jpg)
A decision tree model
1:2≤ >
2:3≤ >
1:3≤ >
|1,2,3|1:3
≤ >2:3
≤ >
|1,3,2| |3,1,2| |2,3,1| |3,2,1|
|2,1,3|
[7, 12, 3]
![Page 15: Lower Bound on Sorting](https://reader033.vdocuments.site/reader033/viewer/2022052603/56813056550346895d960cf1/html5/thumbnails/15.jpg)
A decision tree model
1:2≤ >
2:3≤ >
1:3≤ >
|1,2,3|1:3
≤ >2:3
≤ >
|1,3,2| |3,1,2| |2,3,1| |3,2,1|
|2,1,3|
[7, 12, 3]
![Page 16: Lower Bound on Sorting](https://reader033.vdocuments.site/reader033/viewer/2022052603/56813056550346895d960cf1/html5/thumbnails/16.jpg)
A decision tree model
1:2≤ >
2:3≤ >
1:3≤ >
|1,2,3|1:3
≤ >2:3
≤ >
|1,3,2| |3,1,2| |2,3,1| |3,2,1|
|2,1,3|
[7, 12, 3]
![Page 17: Lower Bound on Sorting](https://reader033.vdocuments.site/reader033/viewer/2022052603/56813056550346895d960cf1/html5/thumbnails/17.jpg)
A decision tree model
1:2≤ >
2:3≤ >
1:3≤ >
|1,2,3|1:3
≤ >2:3
≤ >
|1,3,2| |3,1,2| |2,3,1| |3,2,1|
|2,1,3|
[7, 12, 3]
![Page 18: Lower Bound on Sorting](https://reader033.vdocuments.site/reader033/viewer/2022052603/56813056550346895d960cf1/html5/thumbnails/18.jpg)
A decision tree model
1:2≤ >
2:3≤ >
1:3≤ >
|1,2,3|1:3
≤ >2:3
≤ >
|1,3,2| |3,1,2| |2,3,1| |3,2,1|
|2,1,3|
[7, 12, 3] [3, 7, 12]
![Page 19: Lower Bound on Sorting](https://reader033.vdocuments.site/reader033/viewer/2022052603/56813056550346895d960cf1/html5/thumbnails/19.jpg)
How many leaves are in a decision tree?
Leaves must have all possible permutations of the input
What if decision tree model didn’t? Some input would exist that didn’t have a correct
reordering For an input of size n, how many leaves?
n! leaves
![Page 20: Lower Bound on Sorting](https://reader033.vdocuments.site/reader033/viewer/2022052603/56813056550346895d960cf1/html5/thumbnails/20.jpg)
A lower bound
What is the worst-case number of comparisons for a tree?
1:2≤ >
2:3≤ >
1:3≤ >
|1,2,3|1:3
≤ >2:3
≤ >
|1,3,2| |3,1,2| |2,3,1| |3,2,1|
|2,1,3|
![Page 21: Lower Bound on Sorting](https://reader033.vdocuments.site/reader033/viewer/2022052603/56813056550346895d960cf1/html5/thumbnails/21.jpg)
A lower bound
The longest path in the tree, i.e. the height
1:2≤ >
2:3≤ >
1:3≤ >
|1,2,3|1:3
≤ >2:3
≤ >
|1,3,2| |3,1,2| |2,3,1| |3,2,1|
|2,1,3|
![Page 22: Lower Bound on Sorting](https://reader033.vdocuments.site/reader033/viewer/2022052603/56813056550346895d960cf1/html5/thumbnails/22.jpg)
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
!2 nh
!lognh log is monotonically increasing
)log( nnh
![Page 23: Lower Bound on Sorting](https://reader033.vdocuments.site/reader033/viewer/2022052603/56813056550346895d960cf1/html5/thumbnails/23.jpg)
Other heaps
![Page 24: Lower Bound on Sorting](https://reader033.vdocuments.site/reader033/viewer/2022052603/56813056550346895d960cf1/html5/thumbnails/24.jpg)
Other heaps
![Page 25: Lower Bound on Sorting](https://reader033.vdocuments.site/reader033/viewer/2022052603/56813056550346895d960cf1/html5/thumbnails/25.jpg)
Other heaps