Transcript .ppt

Announcements
• Exam stats
• P3 due on Thursday
CS 100
Lecture 13
1
Today’s Topics
• Discussion of Prelim
• Review of sorting algorithms from last
week
• Iterative Quicksort
• Tomorrow: Matlab begins
CS 100
Lecture 13
2
Prelim Highlights
• Classes/Subclasses/Inheritance
• Method invocations
• b1 = true;
b2 = false;
b1 = b1 && b2 means that now b1 is false
CS 100
Lecture 13
3
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.
CS 100
Lecture 13
4
Question 2
• Remember how aliases work, when an
assignment like c1 = c2; occurs.
• Output of the code
54
is:
93
8 13
26 5
26 5
26 5
79
79
CS 100
Lecture 13
5
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;
}
}
CS 100
Lecture 13
6
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);
}
Lecture 13
7
} CS 100
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;
}
CS 100
Lecture 13
8
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.
CS 100
Lecture 13
9
Question 5, continued
Box for the class C
3
x
An instance of C
y
10
C
b
ourMethod
w ______
z ___4___
An instance of C
y
4
c
ourMethod
C
CS 100
Lecture 13
10
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();
}
CS 100
Lecture 13
11
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)
CS 100
Lecture 13
12
Iterative quicksort (no recursion)
• After partitioning the array, it looks like:
h
b <= x
j
x
k
>x
• 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”.
CS 100
Lecture 13
13
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;}
}
CS 100
Lecture 13
14
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]
CS 100
Lecture 13
15
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;}}}
CS 100
Lecture 13
16
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!
CS 100
Lecture 13
17
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!
CS 100
Lecture 13
18
Changes to algorithm
• Changes to ensure that array c never gets
bigger than log (l-f). If the array has 250 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:
CS 100
Lecture 13
19
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;
}}
CS 100
Lecture 13
20