Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please I need a solution for question 2 Problem 1. Recursive binary search Implement recursive binary search using the following function prototype: int binarySearch(const int

Please I need a solution for question 2 image text in transcribed

Problem 1. Recursive binary search Implement recursive binary search using the following function prototype: int binarySearch(const int A[], int left, int right, int e) The function returns the index of e in A if found between left and right inclusive, or -1 if not found. It assumes the array A is sorted. Binary search takes advantage of the ordered array property: it checks whether the middle element is equal to e, and if it is, it returns the middle element's index. Otherwise, if e is smaller than the middle element it returns the result of binary search on the left part of the array (between left and mid-1). Otherwise, if e is larger than the middle element it returns the result of the binary search on the right part of the array (between mid+1 and right). Finally, if element e is not between left and right, then binarySearch returns - 1. Implement binary search recursively in a separate function and test it by calling it from main. Remember that such functions can run into an infinite recursion if you do not provide appropriate base cases. 1 Problem 2. Binary Search, base case modified The version of binary search in Problem 1 keeps on dividing the array in halves until we either find the element we are searching for or we reach an empty array when said element is missing from the array. Modify the base case of binary search by performing a sequential search when the subarray size becomes less than or equal to 4. That is, we keep on dividing in halves until either we find the element we are after or we get a subarray of size less than or equal to 4; in which case we perform a sequential search. Implement the function int binarySearch4(const int A[], int n, int x) The function is supposed to return the index of 1 in A if found and -1 otherwise. Test it on the array of odd numbers 1, 3, 5, 7, 9, ...,199,201 and use the function to search for the numbers -2, 2, 13, 20, 55, 99, 157, 180, 183, 199, 200, 250

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

What is her relationship to you, both by position and inclination?

Answered: 1 week ago