Searching
 | Binary search
|

Recursion
 | C++ version
int search (int sval, int a[], int low, int high) {
if (low <= high) {
midpt = (low + high)/2;
if (sval < a[midpt])
search (sval, a, low, midpt-1);
else
if (sval > a[midpt])
search (sval, a, midpt+1, high);
else
return 1;
return 0;
}
} |
|
 | Recursion in general
 | A recursive function almost never contains a loop. (It calls itself rather than
looping.) |
 | To write a recursive function
- Decide on the stopping condition(s)..
- Write the code for the stopping condition(s).
- Write the code for the continuing condition(s).
|
|
 | Run-time efficiency vs. programmer time efficiency |
|