Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 100Lecture 131 Announcements Exam stats P3 due on Thursday.

Similar presentations


Presentation on theme: "CS 100Lecture 131 Announcements Exam stats P3 due on Thursday."— Presentation transcript:

1 CS 100Lecture 131 Announcements Exam stats P3 due on Thursday

2 CS 100Lecture 132 Today’s Topics Discussion of Prelim Review of sorting algorithms from last week Iterative Quicksort Tomorrow: Matlab begins

3 CS 100Lecture 133 Prelim Highlights Classes/Subclasses/Inheritance Method invocations b1 = true; b2 = false; b1 = b1 && b2 means that now b1 is false

4 CS 100Lecture 134 Question 1 A) double[] b = { 5.0, 6.2, 3.7 } ; B) if (x > y) min = x; else min = y; C) Binary search requires sorted input, linear search does not. Binary search is much faster than linear search.

5 CS 100Lecture 135 Question 2 Remember how aliases work, when an assignment like c1 = c2; occurs. Output of the code is: 5 4 9 3 8 13 26 5 7 9

6 CS 100Lecture 136 Question 3 public class geometricObject { public int numSides; // number of sides of the object private String color; // color of the object public geometricObject (int sides, String c) { numSides = sides; color = c; } public String getColor () { return color; }

7 CS 100Lecture 137 Question 3, continued public class Rectangle extends geometricObject{ public int length; // length of the rectangle public int width; // width of the rectangle public Rectangle(int userLength, int userWidth, String color){ super(4, color); length = userLength; width = userWidth; } public int area() { return length*width;} } public class Square extends Rectangle { public Square(int side, String color) { super(side, side, color); }

8 CS 100Lecture 138 Question 4 public int numLess(int []b, int x) { int i = 0; int num = 0; while (i < b.length) { if (b[i] <= x) num++; i++; } return num; }

9 CS 100Lecture 139 Question 5 Allocate a new set of locations to contain the parameters and local variables of the method being called, in what we call a frame. Assign the arguments of the call to the parameters of the method. Execute the procedure body. Delete the frame produced in step 1; if the method is a function return the value of the function to the place of the call.

10 CS 100Lecture 1310 Question 5, continued b c 3 x 4 y Box for the class C An instance of C 10 y w ______ z ___4___ ourMethod C C

11 CS 100Lecture 1311 Question 6 public static void main (String args[]) { ATM my_atm = new ATM(); my_atm.balance = 250; my_atm.printBalance(); my_atm.withdraw(200); my_atm.printBalance(); }

12 CS 100Lecture 1312 Review of quicksort Given an array b[0..k] –if b.length = 1, done –if b.length = 2, then swap if necessary, done. –partition the array around b[0], suppose value in b[0] ends up in position j –quicksort b[0..j-1] –quicksort b[j+1..k] This version of quicksort is recursive (calls itself)

13 CS 100Lecture 1313 Iterative quicksort (no recursion) After partitioning the array, it looks like: There are now two sections to sort, b[h..j-1] and b[j+1..k], and while one is being sorted, it must be remembered to sort the other. Sorting b[h..j-1] will result in partitioning and the creation of two other sections to sort; these must also be “remembered”. x h j k b

14 CS 100Lecture 1314 Create a class Bounds // An instance represents the bound f and l of an // array section b[f..l] (for some array) public class Bounds { public int f; public int l; // Constructor: instance with f=fp and l=lp public Bounds(int fp, int lp) {f= fp; l= lp;} }

15 CS 100Lecture 1315 public void Quicksort(int [ ] b, int h, int k) { Bounds c [ ] = new Bounds [ k+1-h]; System.out.println(k); System.out.println(h); c[0]= new Bounds(h,k); int i= 1; // inv: b[h..k] is sorted iff all its subsegments // defined by elements of c[0..i-1] are sorted while (i > 0) { i = i -1; int f= c[i].f; int l= c[i].l; // Process segment b[f..l]

16 CS 100Lecture 1316 if (l-f==1) {// b[f..l] has two elements if (b[f] > b[l]) { // Swap b[f] and b[l] int t= b[f]; b[f]= b[l]; b[l]= t; }} else if (l-f>1) { //b[f..l] has > 2 elements // Add bounds of b[f..j-1] and b[j+1..k] to c int j= partition(b,f,l); c[i]= new Bounds(f,j-1); i= i+1; c[i]= new Bounds(j+1,l); i= i+1;}}}

17 CS 100Lecture 1317 Size of array c How big can array c get? Let b[h..k] have n values, and let b be already sorted. At each step, b[f..j-1] would be empty and b[j+1..k]would have all but one of the elements. After 3 loop iterations, we would have c[0] represents a segment of 0 elements c[1] represents a segment of 0 elements c[2] represents a segment of 0 elements c[3] represents a segment of n-3 elements In worst case array c needs almost n array elements!

18 CS 100Lecture 1318 How to fix this... Put largest of the two segments b[f..j-1], b[j+1..k] on c first, then the smaller. Then, we can show that that if c[0] represents a segment of m elements, c looks like c[0] represents m elements c[1] represents < m/2 elements c[2] represents < m/4 elements c[3] represents < m/8 elements … c[i-1] represents m/ 2 i-1 elements c has at most 1+ log m elements So c has at most 1 + log n elements. Much better!

19 CS 100Lecture 1319 Changes to algorithm Changes to ensure that array c never gets bigger than log (l-f). If the array has 2 50 elements, array c need have no more than 50 elements. 1. Change allocation of c to Bounds c [ ] = new Bounds [ 50]; 2. Change implementation of “Add bounds …” to the following:

20 CS 100Lecture 1320 Code Modifications // Add bounds of b[f..j-1] and b[j+1..k] to c // --put larger segment on first if (j-f > l-j) { c[i]= new Bounds (f,j-1); i= i+1; c[i+1]= new Bounds(j+1..k); i= i+1; } else { c[i]= new Bounds (j+1..k); i= i+1; c[i]= new Bounds(f,j-1); i= i+1; }


Download ppt "CS 100Lecture 131 Announcements Exam stats P3 due on Thursday."

Similar presentations


Ads by Google