Binary search can be implemented as a recursive algorithm. Each call makes a recursive call on one-half of the list the call received as an argument. Complete the recursive function BinarySearch() with the following specifications: Parameters: a target integer a vector of integers lower and upper bounds within which the recursive call will search Return value: the index within the vector where the target is located -1 if target is not found The template provides the main program and a helper function that reads a vector from input. The algorithm begins by choosing an index midway between the lower and upper bounds. If target == integers.at(index) return index If lower == upper, return -1 to indicate not found Otherwise call the function recursively on half the vector parameter: If integers.at(index) < target, search the vector from index + 1 to upper If integers.at(index) > target, search the vector from lower to index - 1 The vector must be ordered, but duplicates are allowed. Once the search algorithm works correctly, add the following to BinarySearch(): Count the number of calls to BinarySearch(). Count the number of times when the target is compared to an element of the vector. Note: lower == upper should not be counted. Hint: Use a global variable to count calls and comparisons. The input of the program consists of: the number of integers in the vector the integers in the vector the target to be located Ex: If the input is: 9 1 2 3 4 5 6 7 8 9 2 the output is: index: 1, recursions: 2, comparisons: 3 ***COULD YOU DEBUG THE CODE. I INCLUDED THE OUTPUT IMAGES SO YOU CAN SEE WHERE IT DIFFERES*** #include #include #include

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

15.14 LAB: Binary search

 

Binary search can be implemented as a recursive algorithm. Each call makes a recursive call on one-half of the list the call received as an argument.

Complete the recursive function BinarySearch() with the following specifications:

  1. Parameters:
    • a target integer
    • a vector of integers
    • lower and upper bounds within which the recursive call will search
  2. Return value:
    • the index within the vector where the target is located
    • -1 if target is not found

The template provides the main program and a helper function that reads a vector from input.

The algorithm begins by choosing an index midway between the lower and upper bounds.

  1. If target == integers.at(index) return index
  2. If lower == upper, return -1 to indicate not found
  3. Otherwise call the function recursively on half the vector parameter:
    • If integers.at(index) < target, search the vector from index + 1 to upper
    • If integers.at(index) > target, search the vector from lower to index - 1

The vector must be ordered, but duplicates are allowed.

Once the search algorithm works correctly, add the following to BinarySearch():

  1. Count the number of calls to BinarySearch().
  2. Count the number of times when the target is compared to an element of the vector. Note: lower == upper should not be counted.

Hint: Use a global variable to count calls and comparisons.

The input of the program consists of:

  1. the number of integers in the vector
  2. the integers in the vector
  3. the target to be located

Ex: If the input is:

9 1 2 3 4 5 6 7 8 9 2

the output is:

index: 1, recursions: 2, comparisons: 3

***COULD YOU DEBUG THE CODE. I INCLUDED THE OUTPUT IMAGES SO YOU CAN SEE WHERE IT DIFFERES***

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int recursions = 0;
int comparisions = 0;// Read integers from input and store them in a vector.
// Return the vector.
vector<int> ReadIntegers() {
   int size;
   cin >> size;
   vector<int> integers(size);
   for (int i = 0; i < size; ++i) {               // Read the numbers
      cin >> integers.at(i);
      }
   sort(integers.begin(), integers.end());
   return integers;
}

 

 

int BinarySearch(int target, vector<int> integers, int lower, int upper) {
   recursions++;
     if (lower > upper)  
   {  
       return -1;
     }  
   int mid = (lower + upper) / 2;
     if (integers[mid] == target) 
    {  
       comparisions++;
          return mid; 
    }  
   else if (integers[mid] > target)  
   {   
      comparisions += 2; 
        return BinarySearch(target, integers, lower, mid - 1);
     }  
   else   
  {  
       comparisions += 2;  
       return BinarySearch(target, integers, mid + 1, upper);
     }
}

int main() {
   int target;
   int index;

   vector<int> integers = ReadIntegers();

   cin >> target;

   index = BinarySearch(target, integers, 0, integers.size() - 1);
   printf("index: %d, recursions: %d, comparisons: %d\n",
          index, recursions, comparisions);
   return 0;
}

I AM GETTING THIS ERROR WHEN THE PROGRAM RUNS(SOME ARE CORRECT AND SOME ARE NOT):

1:Compare output
Input
9 1 2 3 4 5 6 7 8 9 2
Your output
index: 1, recursions: 2, comparisons: 3
 
2:Compare output
0 / 2
Output differs. See highlights below.
Input
9 11 22 33 44 55 66 77 88 99 11
Your output
index: 0, recursions: 3, comparisons: 4
Expected output
index: 0, recursions: 3, comparisons: 5
 
3:Compare output
0 / 2
Output differs. See highlights below.
Input
8 10 15 20 25 30 35 40 45 50
Your output
index: -1, recursions: 3, comparisons: 3
Expected output
index: -1, recursions: 4, comparisons: 7
 
4:Compare output
0 / 2
Output differs. See highlights below.
Input
13 10 20 20 20 20 20 25 30 35 40 45 50 60 20
Your output
index: 3, recursions: 1, comparisons: 2
Expected output
index: 2, recursions: 2, comparisons: 3
 
5:Unit test
2 / 2
Test BinarySearch(99, [11 22 33 44 55 66 77 88 99], 0, 8). Should return 8.
Test feedback
BinarySearch(99, [11 22 33 44 55 66 77 88 99], 0, 8) correctly returned 8
4:Compare output
Output differs. See highlights below.
Input
5:Unit test
Your output
13
10 20 20 20 20 20 25 30 35 40 45 50 60
20
index:
Expected output index: 2, recursions:
Test feedback
recursions:
comparisons: 2
comparisons: 3
Test BinarySearch (99, [11 22 33 44 55 66 77 88 99], 0, 8). Should return 8.
0/2
2/2
inarySearch (99, [11 22 33 44 55 66 77 88 991, 0, 8) correctly returned 8
Transcribed Image Text:4:Compare output Output differs. See highlights below. Input 5:Unit test Your output 13 10 20 20 20 20 20 25 30 35 40 45 50 60 20 index: Expected output index: 2, recursions: Test feedback recursions: comparisons: 2 comparisons: 3 Test BinarySearch (99, [11 22 33 44 55 66 77 88 99], 0, 8). Should return 8. 0/2 2/2 inarySearch (99, [11 22 33 44 55 66 77 88 991, 0, 8) correctly returned 8
Latest submission - 12:37 PM EST on 02/27/23
Only show failing tests
1:Compare output
Input
Your output
2:Compare output ^
Input
Output differs. See highlights below.
Your output
9
1 2 3 4 5 6 7 8 9
2
Expected output
index: 1, recursions: 2, comparisons: 3
9
11 22 33 44 55 66 77 88 99
11
index: 0, recursions: 3, comparisons: 4
index: 0, recursions: 3, comparisons:
сл
Total score: 4 / 10
Download this submission
2/2
0/2
Transcribed Image Text:Latest submission - 12:37 PM EST on 02/27/23 Only show failing tests 1:Compare output Input Your output 2:Compare output ^ Input Output differs. See highlights below. Your output 9 1 2 3 4 5 6 7 8 9 2 Expected output index: 1, recursions: 2, comparisons: 3 9 11 22 33 44 55 66 77 88 99 11 index: 0, recursions: 3, comparisons: 4 index: 0, recursions: 3, comparisons: сл Total score: 4 / 10 Download this submission 2/2 0/2
Expert Solution
Step 1

An array must first be sorted before using the widely used searching algorithm known as binary search. This algorithm's main principle is to divide an array in half repeatedly (divide and conquer) until either the element is found or all the elements have been used up.
It operates by comparing the middle item of the array with our target; if they match, it returns true; if they don't, the left sub-array is searched.

The search is carried out in the appropriate sub-array if the middle term is less than the target.

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Array
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
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