Lecture 7 - Department of Computer Science

1
CompSci 105 SS 2005
Principles of Computer Science
Assignment
1
due Today!!!
Date:
14.1.05
“Not all recursive
solutions are better
than iterative
solutions…”
“… recursion, however,
can provide
elegantly simple
solutions to
problems of great
complexity”
- Textbook, p. 49
Lecture 7: Searching via Recursion
Lecturer: Santokh Singh
n
=3
A: fact(n-1) = ?
return
=?
A
n
=2
A: fact(n-1) = ?
return
=?
public static int fact( int n ) {
if ( n == 0 ) {
return 1;
}
else {
A n-1 );
return n * fact(
}
}
2
A
n
=1
A: fact(n-1) = ?
return
=?
A
n
=0
A: fact(n-1) = ?
return
=?
Textbook, p. 57-58
3
Proving recursive algorithms
1. Prove each base cases works
2. Prove each recursive case works*
3. Prove all recursive calls meet a base case
public static int fact( int n ) {
if ( n == 0 ) {
return 1;
}
else {
return n * fact( n-1 );
}
}
Proof by induction
Textbook, p. 751-5
4
Invariants
public static int fact( int n ) {
if ( n == 0 ) {
return 1;
}
else {
// Invariant:
return n * fact( n-1 );
}
}
Textbook, p. 56
5
Reversing a String
“ d o g ”
• If string is empty
do nothing
• Otherwise,
Output last character
Reverse substring of
first (n-1) characters
Textbook, p. 59ff
Recursive Decimal to Binary Conversion
by student “Charlie” during his CS 105 here
//General Description: Converts a decimal number to
//
Binary representation.
//Precondition:
Input is a positive decimal
//
number.
//Postcondition:
Returns Binary representation
//
of input.
//----------------------------------------------------private static String binVal(long decVal){
String retString = "";
if(decVal / 2 > 0) retString = binVal(decVal / 2);
retString += (decVal % 2);
return retString;
}
6
7
Loop invariants
// Computes the sum of
// the first n items.
int sum = 0;
int j = 0;
while (j < n) {
sum += item[j];
j++;
}
Is there
something that
we want to be
true every time
the while test is
executed?
Textbook, p. 10
// Computes the sum of
// the first n items.
int sum = 0;
int j = 0;
while (j < n) {
sum += item[j];
j++;
}
8
Loop Invariants:
j is in the
range
0..n-1
sum contains
the sum of the
first j items
Textbook, p. 10
// Computes the sum of
// the first n items.
int sum = 0;
int j = 0;
while (j < n) {
sum += item[j];
j++;
}
Loop Invariants:
j is in the
range
0..n-1
sum contains
the sum of the
first j items
9
0
item:
n:
1
1 3
2
j:
sum:
Textbook, p. 10
// Computes the sum of
// the first n items.
int sum = 0;
int j = 0;
while (j <= n) {
sum += item[j];
j++;
}
10
an error
Loop Invariants:
j is in the
range
0..n
sum contains
the sum of the
first j items
Textbook, p. 10
11
Searching in a sorted array
Binary search
Searching in an unsorted array
Finding the smallest item
Finding the kth smallest item
12
Divide and Conquer
Search
Phone Directory
Search 1st Half of
Phone Directory
Search 2nd Half of
Phone Directory
Textbook, p. 50
13
Binary Search
Search
Phone Directory
Search 1st Half of
Phone Directory
Search 2nd Half of
Phone Directory
Textbook, pp. 77ff
14
Binary Search Algorithm
bSearch( anArray, value)
if (anArray is of size 1)
Is anArray’s item = to value?
else
Find midpoint of anArray
Which half of anArray has value?
bSearch( halfOfAnArray, value)
15
Binary Search Algorithm
bSearch( anArray, value)
if (anArray is of size 1)
Is anArray’s item = to value?
else
Find midpoint of anArray
Which half of anArray has value?
bSearch( halfOfAnArray, value)
Textbook, pp. 78
16
value:
0
anArray:
1
2
3
4
1 3 7 8 9
Textbook, pp. 78
17
value:
0
anArray:
1
2
3
4
1 3 7 8 9
first:
last:
Textbook, pp. 78
18
value:
0
anArray:
1
2
3
4
1 3 7 8 9
first:
last:
mid:
Textbook, pp. 80
19
bSearch( anArray, value, first, last )
if (last == first)
Is anArray’s item = to value?
else
mid = (first + last) / 2
if (value is in first half )
bSearch( anArray,value,first,mid)
else
bSearch( anArray,value,mid+1,last)
Textbook, pp. 78
20
Searching in a sorted array
Binary search
Searching in an unsorted array
Finding the smallest item
Finding the kth smallest item
21
Unsorted array: Find Smallest
Find smallest
Find smallest in
First half
0
Find smallest in
Second half
1
2
3
4
8 3 9 1 7
Textbook, pp. 76
22
Unsorted array: Find kth Smallest
Find kth smallest
Find kth smallest in
First half
0
Find kth smallest in
Second half
1
2
3
4
8 3 9 1 7
Textbook, pp. 81ff
23
Please remember to write
YOUR name and login/ID
on your assignment 1
answers’ booklet.