RecursionBinarySearch Flashcards
What is a recursive function?
A fn that calls itself.
What is the base case in recursion?
The base case is the part of the function
that determines when to stop and does not
make a recursive call.
What is the typical recursive pattern?
void factorial(int n) {
if(n < 0) {
return -1;
} else if(n == 0 || n == 1) {
return 1;
} else {
int rest = n - 1;
int factRest = factorial(rest);
return n * factRest;
}
}
How does binary search work?
Begin with a start index of 0 and an end index
of length-1.
○ Examine the value at the mid point (half way
between the start and the end).
■ If the mid value is the target, return true.
■ If the mid value is larger than the target,
set end to mid - 1.
■ If the mid value is less than the target,
set start to mid + 1.
○ Repeat until the target is found, or start >
end.