1
“Not all recursive solutions are better than iterative solutions…”
“… recursion, however, can provide elegantly simple solutions to problems of great complexity”
- Textbook, p. 49
CompSci 105 SS 2005
Principles of Computer Science
Lecture 7: Searching via Recursion
Lecturer: Santokh Singh
Assignment 1
due Today!!!
Date:14.1.05
2public static int fact( int n ) {
if ( n == 0 ) {
return 1;
}
else {
return n * fact( n-1 );
}
}
Textbook, p. 57-58
n = 0 A: fact(n-1) = ?return = ?
n = 1A: fact(n-1) = ?return = ?
n = 3A: fact(n-1) = ?return = ?
n = 2A: fact(n-1) = ?return = ?
A
A
A
A
3
Proving recursive algorithms
1. Prove each base cases works
2. Prove each recursive case works*
3. Prove all recursive calls meet a base casepublic static int fact( int n ) {
if ( n == 0 ) {
return 1;
}
else {
return n * fact( n-1 );
}
} Proof by inductionTextbook, 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
Textbook, p. 59ff
god“ ”• If string is empty
do nothing• Otherwise,
Output last character
Reverse substring of first (n-1) characters
6
//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; }
Recursive Decimal to Binary Conversionby student “Charlie” during his CS 105 here
7
Loop invariants
Is there something that we want to be true every time the while test is executed?
// Computes the sum of
// the first n items.
int sum = 0;
int j = 0;
while (j < n) {
sum += item[j];
j++;
}
Textbook, p. 10
8
sum contains the sum of the first j items
j is in the range 0..n-1
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:
9
sum contains the sum of the first j items
j is in the range 0..n-1
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:
31item:
j:
n: 2
0 1
sum:
10
sum contains the sum of the first j items
j is in the range 0..n
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:
an error
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
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)
Binary Search Algorithm
15
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)
Binary Search Algorithm
Textbook, pp. 78
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 inFirst half
Find smallest inSecond half
938 710 1 2 3 4
Textbook, pp. 76
22
Unsorted array: Find kth Smallest
Find kth smallest
Find kth smallest inFirst half
Find kth smallest inSecond half
938 710 1 2 3 4
Textbook, pp. 81ff
Top Related