Download presentation
Presentation is loading. Please wait.
Published byClaude Daniel Modified over 9 years ago
1
CSC 213 Lecture 13: Writing Code & Sorting Lowest Bound
2
Writing Code Need Not Be Hard Writing code can be difficult Need to figure out how to start, what to do next, how to test, etc. But you already know many things Algorithms, how to draw concepts, what you will need for the node class Key is to use what these ideas, pictures, and algorithms to do the hard work
3
Our Node Class public class Node { // Usual fields, constructors included public void setLeft(Node n) {... } public void setRight(Node n) {... } public void setParent(Node n) {... } public void setRed(boolean b) {... } public Node getLeft() {... } public Node getRight() {... } public Node getParent() {... } public boolean getRed() {... } public boolean isLeftChild() {... } public int getHeight() {... } }
4
Working from Concepts We can always create imaginary situations and draw the picture Suppose we must write rightRightSplay Make simple tree that provides examples Note: This is also an excellent test case
5
Working from Concepts Suppose we must write rightRightSplay Draw Splay tree after we insert 88 Note: This is also a good test case 44 17 50 62
6
Working From Analogues May find similar, but not identical, example Could try working from picture or draw actual example we are interested in Code rightRotate and have this picture
7
Working From Pictures Easiest case: work from picture in book Now lets write rightLeftDRed(Node z)
8
Time Needed for n 2 Sorts
11
Time Needed for n log n Sorts
14
Comparison-Based Sorting (§ 10.3) Most sorting algorithms compare elements Examples: bubble-sort, selection-sort, insertion- sort, heap-sort, merge-sort, quick-sort,... What is minimum running time of algorithm that sorts n elements, x 1, x 2, …, x n ? Is x i < x j ? yes no
15
Counting Comparisons Consider a run as path in decision tree We will only count number of comparisons Height of tree is lower-bound on running time Is x i < x j ? yes no
16
Decision Tree Height Each input combination leads to unique leaf node Else make same decisions for two input combinations E.g. In sequence 4, 5 and sequence 5, 4 we determine (4 < 5) == (5 < 4) How many different ways can we arrange n numbers in a Sequence?
17
Decision Tree Height If there are n ! leaf nodes, the decision tree height is _______
18
The Lower Bound Comparison-based sorting algorithms takes at least log( n !) time, but: n ! = n * n-1 * n-2 * n-3 * … * 2 * 1 n ! > (n/2) n/2, since n / 2 numbers bigger than n / 2 log( n !) > log (n/2) n/2 log (n/2) n/2 = n/2 * log (n/2) So, log( n !) > n/2 * log (n/2)
19
Lower Bound on Sorting Minimum number of comparisons is equal to the height of the tree The height of the tree is log( n !) But log( n !) > n/2 * log (n/2) So, height of tree is larger than O ( n log n ) Therefore, any comparison-based sort will take at least O ( n log n ) time!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.