Embed presentation
Download to read offline
![(C++ exercise) 4. Write a recursive version of the binary search
algorithm. (Hint: There will be two recursive function calls in
the function.)
P/S: please seperate the main.cpp, header file and implement
file (if there is any header and implement file that may apply)
like this pattern as shown below:
main.cpp
Code
header.h
Code
implement.cpp
Code
(Thanks for helping me and I really do appriciate it!)
Solution
#include <iostream>
#include <cmath>
using namespace std;
// the function
int binarySearch(int L[],int x, int first, int last)
{
if (last >= first) //first > last)
{
//return -1;](https://image.slidesharecdn.com/cexercise4-220914224014-489b015b/75/C-exercise-4-Write-a-recursive-version-of-the-binary-search-alg-docx-1-2048.jpg)
![int middle = (first + last) / 2;
if (x == L[middle])
return middle;
else if (x < L[middle])
return binarySearch(L, x, first, middle - 1);
else //if(x > L[middle])
return binarySearch(L, x, middle + 1, last);
}
else
return -1;//(first + 1); // failed to find key
}
int main()
{
/* int myList[size] = n;
int myfirst = 0;
int mylast = n - 1;
int findthis;*/
int myList[] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19};
//int size;
//int myList[size];
//int n = myList[10];
int myfirst = 0;
int mylast = 10;//n - 1;
int findthis;](https://image.slidesharecdn.com/cexercise4-220914224014-489b015b/75/C-exercise-4-Write-a-recursive-version-of-the-binary-search-alg-docx-2-2048.jpg)

The document provides a recursive implementation of the binary search algorithm in C++. It includes the function definition for binarySearch() that takes an array, search value, and start and end indices as parameters. The function recursively calls itself, narrowing the search range by half each time to check if the value is equal to the middle element, less than the middle, or greater than the middle. It also includes a main() function that demonstrates calling binarySearch() to search a sample array for a user-input value and output the result.
![(C++ exercise) 4. Write a recursive version of the binary search
algorithm. (Hint: There will be two recursive function calls in
the function.)
P/S: please seperate the main.cpp, header file and implement
file (if there is any header and implement file that may apply)
like this pattern as shown below:
main.cpp
Code
header.h
Code
implement.cpp
Code
(Thanks for helping me and I really do appriciate it!)
Solution
#include <iostream>
#include <cmath>
using namespace std;
// the function
int binarySearch(int L[],int x, int first, int last)
{
if (last >= first) //first > last)
{
//return -1;](https://image.slidesharecdn.com/cexercise4-220914224014-489b015b/75/C-exercise-4-Write-a-recursive-version-of-the-binary-search-alg-docx-1-2048.jpg)
![int middle = (first + last) / 2;
if (x == L[middle])
return middle;
else if (x < L[middle])
return binarySearch(L, x, first, middle - 1);
else //if(x > L[middle])
return binarySearch(L, x, middle + 1, last);
}
else
return -1;//(first + 1); // failed to find key
}
int main()
{
/* int myList[size] = n;
int myfirst = 0;
int mylast = n - 1;
int findthis;*/
int myList[] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19};
//int size;
//int myList[size];
//int n = myList[10];
int myfirst = 0;
int mylast = 10;//n - 1;
int findthis;](https://image.slidesharecdn.com/cexercise4-220914224014-489b015b/75/C-exercise-4-Write-a-recursive-version-of-the-binary-search-alg-docx-2-2048.jpg)
