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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Lists
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education