Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 280 Data Structures Professor John Peterson. Quiz Wrapup interface Comparable { public boolean gtr(T x, T y); } class Student { public int studentNumber;

Similar presentations


Presentation on theme: "CS 280 Data Structures Professor John Peterson. Quiz Wrapup interface Comparable { public boolean gtr(T x, T y); } class Student { public int studentNumber;"— Presentation transcript:

1 CS 280 Data Structures Professor John Peterson

2 Quiz Wrapup interface Comparable { public boolean gtr(T x, T y); } class Student { public int studentNumber; public String studentName; } Comparable to sort students by number. Comparable to sort students by name. Sort students given an array of students and a Comparable object. Write the signature of a general sort method using arrays and a comparable

3 Using Generics in Anger Our last homework was really about interfaces and generics. The best thing you can do is to learn how to read generic types. Let’s look at the solution in Netbeans.

4 heapify void heapify(int i, Sortable s, int n) { // n is the limit of s int l = left(i); // 2*i+1 int r = right(i); // 2*i+2 if (r < n) { if (s.gtr(l, i) || s.gtr(r, i)) { if (s.gtr(l,r)) { s.swap(i,l); heapify(l,s,n); } else { s.swap(i,r); heapify(r,s,n); }} else if (l < n) { if (s.gtr(i,l)) swap(i,l); }

5 Complexity What is the complexity of heapify(0, n)? Why? Let’s work some examples on the board.

6 Heapifying Everything Heapify has a basic problem: It assumes that left and right are already heapified. How can we get everything in the array heapified?????

7 Heapsort Stage 1 void heapsort(Sortable s) { n = s.size(); for (int i = n/2-1; i <= 0; i--) heapify(i, s.size()); ….. } Note that you could use n-1 instead of n/2-1 – this is just an optimization.


Download ppt "CS 280 Data Structures Professor John Peterson. Quiz Wrapup interface Comparable { public boolean gtr(T x, T y); } class Student { public int studentNumber;"

Similar presentations


Ads by Google