
Concept explainers
15.14 LAB: Binary search
Binary search can be implemented as a recursive
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
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 2the output is:
index: 1, recursions: 2, comparisons: 3ATTENTION
WHERE IT SAYS CODE HERE, IS WHERE I MUST ADD CODE. EVERYTHING ELSE CANNOT BE ALTERED
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// 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) {
/* code here. */
}
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, comparisons);
return 0;
}

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

- Recursive Multiplication Given an JavaScript object list of books that each have a pages attribute to define the number of pages in the book, find the product of all the pages in the object list of books using recursion (you must use recursion to solve this problem). Keep in mind: The input list object may be completely empty (ex. {}) The next attribute may not be defined function getPageCount(list) {// your code here// returns an integer}Example test case:Input: {"book":"A","pages":1,"next":{"book":"B","pages":2,"next":{"book":"C","pages":3,"next": null}}}Output: 6Reasoning: 1 * 2 * 3 pagesarrow_forward6) While there is a built-in pop_back() method for vectors, there is no built-in pop_front method. Suppose a program needs a pop_front() method that will remove the first element from the vector. For example if the original vector is [1, 2, 3, 4, 5], then after passing in this vector to pop_front() the new resulting vector will be [2, 3, 4, 5]. Which of the options below is the correct implementation of pop_front()? Group of answer choices D-) void pop_front(vector<int> &v){ for(int i=0; i<(v.size()-1); i++)v[i+1] = v[i]; v.pop_back(); } C-) void pop_front(vector<int> &v){ for(int i=0; i<(v.size()-1); i++)v[i] = v[i+1]; v.pop_back(); } B-) void pop_front(vector<int> &v){ for(int i=0; i<v.size(); i++)v[i+1] = v[i]; v.pop_back(); } A-) void pop_front(vector<int> &v){ for(int i=0; i<v.size(); i++)v[i] = v[i+1]; v.pop_back(); } 7) Suppose a program has the following vector: [99, 23, 55, 71, 87, 64, 35, 42] The goal is to…arrow_forwardnputer Science I| for Majors home > 13.4: Recursive Helper Methods E zyBa Feedb PARTICIPATION 13.4.3: Compute recursively to determine the sum of values in an array. АCTIVITY Assume you have the following method: public static int sum(int [] a) To compute the sum of the values in an array, add the first value to the sum of the remaining values, computing recursively. Rearrange the following lines to design a recursive helper method to solve this problem. Not all lines are useful. Mouse: Drag/drop Keyboard: Grab/release Spacebar (or Enter). Move 0000 Cancel Esc Unused FindSum.java Load default template.. return a[start] + sumHelper(a, start - 1); import java.util.Scanner; return alstart] + sumHelper(a, start + 1); public class FindSum { if (start == a. length) { return a[start); } if (start == a.length - 1) { return alstart]; } public static int sum(int[0 a) if (a.length == 0) { return -1; } return sumHelper(a, 0); if (a.length == 0) { return 0; } public static int sumHelper(int [] a,…arrow_forward
- ii) In JAVA language input the elements of an integer array A of size 10 and find the count of all hills in the array. A[i] is a Hill if A[i-1]A[i+1]. A[i] >= 0 All elements of the array are unique.arrow_forwardCode in C. Solve the code below. Write a function that takes a vector of integers, an integer n representing the number of elements in that vector, and an integer b. Implement the sequential search algorithm to look for the integer b in the vector. The sequential search works as follows: you must look at all the elements of the vector until you find the b, from the first to the last. The function must return an integer representing how many elements the function tested until it found b. If it does not find the function, it must return 0. The name of the function must be called "busca_seq". int busca_seq(int vetor[], int n, int b) { //your code } My Code #include<stdio.h> int busca_seq(int vetor[], int n, int b) { int i; for(i = 0; i < n; i++){ if(vetor[i] == b) return (i+1); } { int vet[5],n = 5,b,x; vet[0] = 1; vet[1] = 2; vet[2] = 3; vet[3] = 4; vet[4] = 5; scanf("%d",&b); x = busca_seq(vet,n,b); printf("%d",x); return 0; } }arrow_forwardDouble linked list in Java Double linked list Textbook reference Data structures and algorithms Micheal Goodricharrow_forward
- Question 11 mahuse c++ Full explain this question and text typing work only We should answer our question within 2 hours takes more time then we will reduce Rating Dont ignore this linearrow_forwardMirrored Matrices Concepts Tested: iterating through multi-dimensional arrays, functions, function overloading Instructions: 1) Write a SET OF 3 OVERLOADED functions called flipMatrixHorizontal() that takes the following inputs: a) a 2-dimensional array with any number of rows and 3, 4, or 5 columns (this is where the overloading comes in). b) an array to put the flipped array values in c) number of rows d) number of columns The function should reverse the ROWS (rotate around the horizontal axis)of the original array and put the new values in the flipped array. 2) Write a SET OF 3 OVERLOADED functions called flipMatrixVertical() that takes the following inputs: a) a 2-dimensional array with any number of rows and 3, 4, or 5 columns (this is where the overloading comes in). b) an array to put the flipped array values in c) number of rows d) number of columns The function should reverse the COLUMNS (rotate around the vertical axis)of the original…arrow_forwardComplete the function: bool search(vector<int> v, int key). This function searches for value key in vector v and return true if key is found else return false. In C++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





