
Modified Recursive Binary Search • Write C++ program hw7.cpp that meets the following requirements: – First, write a recursive binary search function with the following prototype: int binarySearch(int array[], int first, int last, int value); – Now suppose we have a sorted array like this: int a[SIZE] = { 3, 5, 7, 9, 22, 22, 22, 22, 30, 35, 51, 52, 73 }; – Then the following statement int index = binarySearch(a, 0, SIZE-1, 22); – would return one of the indices that match the target value (22), and it could be index 4, 5, 6, or 7. – Now, your mission is to modify this recursive binary search function to always return the leftmost index when there are multiple indices that match the target value. Thus, in the above case, the function must return index 4. – If no match found, the function returns -1. – Write a main function that checks if your modified binary search function works as expected with at least 3 example arrays. – Note: your modified binary search function must ensure the time complexity of O(log2 n), where n is the array size, otherwise will incur huge point deduction (even if the program works). In layman’s terms, it means “no linear search allowed in any part of your program”. Example run: Input: int a[SIZE] = { 3, 5, 7, 9, 22, 22, 22, 22, 30, 35, 51, 52, 73 }; Target: 22 Output: 4 Input: int a[SIZE] = { 3, 3, 3, 9, 12, 15, 20, 35, 51, 73 }; Target: 3 Output: 0 1 Input: int a[SIZE] = { 5, 9, 12, 22, 22, 22, 30, 35, 51, 63, 63 }; Target: 63 Output: 9 Input: int a[SIZE] = { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 }; Target: 9 Output: 0 Input: int a[SIZE] = { 3, 5, 7, 9, 22, 22, 22, 22, 30, 35, 51, 52, 73 }; Target: 25 Output: -1

Trending nowThis is a popular solution!
Step by stepSolved in 4 steps with 2 images

- *Data Structuresarrow_forwardin C programing Write a recursive function that returns 1 if an array of size n is in sorted order and 0 otherwise. Note: If array a stores 3, 6, 7, 7, 12, then isSorted(a, 5) should return 1 . If array b stores 3, 4, 9, 8, then isSorted(b,4) should return 0.int isSorted(int *array, int n){arrow_forwardIn C++, write a program that reads in an array of type int. You may assume that there are fewer than 20 entries in the array. The output must be a two-column list. The first column is a list of the distinct array elements and the second column is the count of the number of occurences of each element.arrow_forward
- f18 In c++arrow_forwardint arr[] = {-2, -4, 8, 4, 3, 3, -9};In Java with this given this array, how would this version of Divide and Conqueror be implemented in this case, specifically?arrow_forwardImplement bubble sort, selection sort iteratively, and recursively. Implement linear search. Familiarize with C++ STL class vector Program Execution: The following example illustrates how your program should work. The program first reads the length of the vector (as 5 in example below), then reads this number of integers, and push them into a vector. The program then reads a command (selection, bubble, rselection, rbubble), and call the corresponding sorting function to sort the vector. The vector after being sorted will be displayed. Note that the user input are underscored. [storm:]$ g++ lab1.cpp -o lab1.exe [ storm:]$ ./lab1.exe 5 74 25 32 99 24 selection 24 25 32 74 99 [storm:]$ ./lab1.exe 317 5 23bubble 5 17 23 General Hints:* Write your main function first. * Incrementally write one function and test it before moving to the next. Please refer to the slides on how to implement these functions. 1. selection: iterative selection sort function bubble: iterative bubble…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





