Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 280 Data Structures Professor John Peterson. Invariants Back to Invariants! Recall the insertion sort invariant – how can we turn this into debugging.

Similar presentations


Presentation on theme: "CS 280 Data Structures Professor John Peterson. Invariants Back to Invariants! Recall the insertion sort invariant – how can we turn this into debugging."— Presentation transcript:

1 CS 280 Data Structures Professor John Peterson

2 Invariants Back to Invariants! Recall the insertion sort invariant – how can we turn this into debugging code?

3 Example i UnchangedSortedV i Unchanged<= VV>V j These pictures are essential documentation! These can easily be turned into code. As invariants get more complex, placing them into the code becomes more useful. for i <- 1 to length[A]-1 do v <- A[i] j <- i-1 while j >= 0 and A[j] > v do A[j + 1] = A[j]; j <- j-1 A[j+1] <- v

4 Netbeans Time Let’s look at a version of insertion sort with some assertions added in the form of conditional prints.

5 Other Sorts Selection Sort – “pin down” each element one at a time. What sort of invariants? Bubble Sort – sweep through the array, swapping out of order pairs. Repeat until no swaps. What sort of invariants?

6 The Java Assert Feature assert ; // Error if false assert : // If false, compute an error msg Assertion failure is an exception – may or may not be caught by the program. The big idea is that there is a flag at the jvm level to enable / disable assertion testing.

7 Assert Early! Suppose you’re about to index an array: i = a[j] There is an implicit assertion here that j >=0 and j < a.length. This is always tested in Java! But where in the program do we know for sure that j must be in this range? Ideally, we place the assertion at any point where j is assigned to leading to this array indexing. Avoid working backward when possible!

8 Fast Sorts All of the sorting algorithms so far have been lame – n^2 is too slow for large data sets. We need to use a better algorithm design to improve performance. We’ll study 3 fast sorts: heapsort, quicksort, and mergesort.

9 Trees A tree is a data structure in which each element is (possibly) linked to 2 or more elements below it. At the top of the tree is the “root”. We’ll be using a binary tree – each node has at most two descendents. Our tree will also be balanced – all leaves (nodes with no descendents) will be the same (almost) distance from the root.

10 Why Trees? The big idea is simple: tree-based algorithms are typically logarithmic instead of linear. The operations we’re interested in depend on the depth of the tree. What is the relationship between tree depth and total number of elements in a balanced tree?

11 Embedding While trees are often build directly using link fields (instance variables) in classes, we can also embed a tree in an array. This is how heapsort works. What does this mean? If we know the location of an element, then some math yields the location of the left and right subtrees.

12 Our Embedding The root node is at location 0 in the array. At node i, the left subtree is at 2*i+1 and the right subtree is at 2*i+2. If you do the math, you’ll see that this fills the array and doesn’t have any “collisions” between different subtrees. Let’s draw this on the board …


Download ppt "CS 280 Data Structures Professor John Peterson. Invariants Back to Invariants! Recall the insertion sort invariant – how can we turn this into debugging."

Similar presentations


Ads by Google