Transcript .ppt

Announcements
•
•
•
•
P2 is due tomorrow
Prelim on Monday
Review Session on Friday in class
Quiz today
CS100
Lecture 11
1
Today’s Topics
• Review
• Search Algorithms
– Linear Search
– Binary Search
CS100
Lecture 11
2
Review of Arrays
•
•
•
•
•
How to declare an array?
What do we use arrays for?
What is the subscript in g[3*x] ?
Initializer lists
Syntax choices
CS100
Lecture 11
3
The Search Problem
• Linear Search: Find (the value of) x in array b.
• This description is too vague, for several reasons:
– How are we to indicate that x appears in b?
– What if it appears more than once in b?
– What is to be done if x is not in b?
• In general, when approaching a new programming job,
the first task is to make the specification of the job as
precise as possible.
CS100
Lecture 11
4
More specifically. . .
• One way to specify the task:
• Write a method that returns the index of the first element
of b that equals x. If x does not occur in b, return
b.length.
• Example: b = {1, 2, 8, 5, 2, 9} (so b.length = 6)
–
–
–
–
If x is 1,
If x is 2,
If x is 5,
if x is 3,
CS100
return 0
return 1
return 3
return 6
Lecture 11
5
How do we construct the algorithm?
// return value i that satisfies: 0<=i<=b.length,
//
x not in b[0..i-1],
//
(i = b.length or x=b[i])
public static int linearSearch (int [ ] b, int x)
Need a loop. It will compare elements of b with x,
beginning with b[0], b[1], …
Invariant: 0<=i<=b.length and x not in b[0..i-1]
0
b
CS100
i
b.length
x is not here
Lecture 11
6
One linear search solution
// return the value i that satisfies: 0<=i<=b.length,
//
x not in b[0..i-1], and (i = b.length or x=b[i])
public static int linearSearch (int [ ] b, int x) {
int i= 0;
// Inv: 0<=i<=b.length and x not in b[0..i-1],
while (i < b.length && x != b[i])
i= i+1;
return i;
}
CS100
Lecture 11
7
A second linear search solution
int i= 0;
// Inv: 0<=i<=b.length and x not in b[0..i-1],
while (i < b.length) {
if (x == b[i])
return i;
i= i+1;
}
return i;
}
CS100
Lecture 11
8
Efficiency
• What does it mean for an algorithm to be
efficient?
• One way is to think about much compute-time is
being spent. How many comparisons are made,
for example?
• There is a whole field of computer science that
looks at analyzing the cost of algorithms
• We’ll just touch on it briefly.
CS100
Lecture 11
9
Cost of Linear Search
• Worst case: We have to make b.length
comparisons
• Can we do better than this?
• Suppose we’re looking for someone’s name
in a NYC phonebook -- would you use
linear search?
CS100
Lecture 11
10
Binary Search
• Assume that the array b is sorted in ascending
order
• Compare the item your interested in to the center
of the list
• If it’s greater, repeat search on one half, if smaller,
repeat on the other half -- and continue. . .
• Each comparison thus eliminates half of the
remaining possibilities
CS100
Lecture 11
11
Simple idea, but . . .
• Method dates back to 1946 -- it wasn’t until
1962 that a bug-free version appeared!
• One study (albeit in ‘83) showed that 90%
of programmers fail to code up binary
search correctly.
• So, we’ll play with some formal algorithmic
stuff for awhile before looking at Java
CS100
Lecture 11
12
Algorithm Development
• Specification. Store an integer in k to truthify:
b[0..k] <= x < b[k+1..b.length-1]
0
b
k
<= x
>x
0 1 2 3 4 5 6 7 8
Examples:
b
x
b.length
2 3 3 3 5 6 8 8 9
1
2
3
4
5
6
7
8
9
10
k -1
0
3
3
4
5
5
7
8
8
CS100
Lecture 11
13
Invariant?
Store an integer in k to truthify
b[0..k] <= x < b[k+1..b.length-1]
0
b
k
b.length
<= x
>x
Invariant
0
k
j
<= x
CS100
b.length
>x
Lecture 11
14
Let’s fill in the blanks
k=
;
j=
;
while (
){
int e =
;
// We know that -1 <= k < e < j <= b.length
if ( b[e] <= x )
else
}
CS100
Lecture 11
15
Binary Search in Java
// Given sorted b, return the integer k that satisfies
// b[0..k] <= x < b[k+1..b.length-1]
public static int binarySearch( int [ ] b) {
int k= -1;
int j= b.length;
// Invariant: -1 <= k < j <= b.length and
//
b[0..k] <= x < b[k+1..b.length]
while (k+1 != j) {
int e = (k+j)/2;
// We know -1 <= k < e < j <= b.length
if ( b[e] <= x ) k= e;
else
j= e;
}
return k;
Lecture 11
}CS100
16
About this algorithm
• It works when the array is empty (b.length = 0) --return -1.
• If x is in b, finds its rightmost occurrence
• If x is not in b, finds position where it belongs
• In general, it’s faster than a binary search that stops as
soon as x is found. The latter kind of binary search needs
an extra test (x = b[k]) in the loop body, which makes each
iteration slower; however, stopping as soon as x is found
can be shown to save only one iteration (on the average).
• It’s easy to verify correctness of the algorithm --to see that
it works.
CS100
Lecture 11
17
How fast is binary search?
b.length
0
= 20 - 1
no.of iterations
log(b.length+1)
0
0
1
= 21 - 1
1
1
3
= 22 - 1
2
2
7
= 23 - 1
3
3
15
= 24 - 1
4
4
15
15
32767 = 215 -1
1048575= 220 -1
20
j is the “base 2 logarithm of 2j.
m is the base 10 logarithm of 10m.
CS100
Lecture 11
20
18
So, how fast?
• To search an array of a million entries using linear
search may require a million iterations of the loop.
• To search an array of a million entries using
binary search requires only 20 iteration!
• Linear search is a linear algorithm; the time it
takes is proportional in the worst case to the size
of the array.
• Binary search is a logarithmic algoririthm; the
time it takes is proportional in the worst case to
the log of the size of the array.
CS100
Lecture 11
19
Classes redux
• Suppose we want to manipulate geometric
objects
• Superclass: geometricObject
• Potential subclasses:
– Triangle
– Square
– Rectangle
CS100
Lecture 11
20
Triangle
sideOne
sideTwo
sideThree
isEquilateral
isRight
area
CS100
The Big Picture
GeometricObject
color
numSides
area
Rectangle
length
width
area
Lecture 11
Square
side
area
isRhombus
21