RecursionBinarySearch Flashcards

1
Q

What is a recursive function?

A

A fn that calls itself.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the base case in recursion?

A

The base case is the part of the function
that determines when to stop and does not
make a recursive call.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the typical recursive pattern?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How does binary search work?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly