Question

In: Computer Science

In c++ A binary search member function of SortedType has the following prototype: void.SortedType::BinarySearch(int value, bool&...

In c++

A binary search member function of SortedType has the following prototype:
void.SortedType::BinarySearch(int value, bool& found);
Write the function definition as a recursive search, assuming an array-based implementation. Add a main function as a driver to test the BinarySearch. Test the edge cases.

Solutions

Expert Solution

#include <bits/stdc++.h>
using namespace std;
int binarySearch(int arr[],int l,int r,int x)
{
if (r >= l)
{
int mid = l + (r - l) / 2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x); //If element is smaller than mid, then it can only be present in left //subarray
return binarySearch(arr, mid + 1, r, x); // Else the element can only be present in right subarray
}
return -1;
}
int main(void)
{
int n;
cout<<"Enter the number of elements-\n";
cin>>n; //reading n
int arr[n+1];
cout<<"Enter the array elements-\n";
for(int i=0;i<n;i++)
{
cin>>arr[i]; //reading array elements
}
int x;
cout<<"Enter the element to be found-\n";
cin>>x;
int result = binarySearch(arr, 0, n - 1, x); // calling function binarySearch
if(result==-1)
cout<<"Element not found";
else
cout<<"Element found at index - "<<result;
return 0;
}



Related Solutions

implement this search function using CPP Coding language bool binarySearch(vector<int>vec,int low,int high, int search) Test your...
implement this search function using CPP Coding language bool binarySearch(vector<int>vec,int low,int high, int search) Test your code in main
[C++ Language] Look at the following pseudo code: Binary_search(int a[], int size) { ……….// binary search...
[C++ Language] Look at the following pseudo code: Binary_search(int a[], int size) { ……….// binary search and return } Selection_Sort(int a[], int z) { …..// do the selection sort } main() {     Selection_Sort(array, size);     Binary_Search(array, item); }
Reimpelment a function called bubble_sort that has the following prototype. bubble_sort(int *array, int size, pointer to...
Reimpelment a function called bubble_sort that has the following prototype. bubble_sort(int *array, int size, pointer to a function) Pre condition array - a pointer to a an array of size element. pointer to function - a pointer to a function that compares two values (depending on sorting in ascending order or descending order) Post condition Sort the array in ascending or descending based on the the pointer to a function. Call the function bubble_sort to sort the array in ascending...
C++ Build a binary tree using a binary tree class member function from the following array...
C++ Build a binary tree using a binary tree class member function from the following array of preorder traversal 3,9,20,15,7 and inorder traversal 9,3,15,20,7. Implement the constructor, destructor and all member functions including print postorder traversal of the binary tree.
Develop an x86 assembly language program that properly executes an "is even" function: bool isEven(int value)...
Develop an x86 assembly language program that properly executes an "is even" function: bool isEven(int value) If the value passed in is even, return 1 If the value passed in is odd, return 0 Remember that the return value must be placed in the EAX register Make sure that any registers (not EAX) used by this function are placed back to their initial states prior to exiting Allow an end user to test this function in the following manner: Prompt...
Given the pseudocode for Binary Search Algorithm as below: BinarySearch(A, p, r, V)    if p...
Given the pseudocode for Binary Search Algorithm as below: BinarySearch(A, p, r, V)    if p < r q = (p + r)/2 if V = A[q] return q else if V > A[q] return BinarySearch(A, q+1, r, V)    else return BinarySearch(A, p, q-1) else if p = r,    if V = A[p]    return p else return -1    return -1 end function Using this pseudocode, write a function for BinarySearch and also complete the program, by...
Write a program of Binary Search in C++ by using function and arrays with the explanation.
Write a program of Binary Search in C++ by using function and arrays with the explanation.
C++ (function code) Binary Search Trees (BST) Code for height, min value, max value, counting node/leaves/full...
C++ (function code) Binary Search Trees (BST) Code for height, min value, max value, counting node/leaves/full nodes
C++ bool exists_trio(int*,int); (it must use this line here) I was not sure how to utilize...
C++ bool exists_trio(int*,int); (it must use this line here) I was not sure how to utilize this line because I made code that works but not with this line specifically. //Input:    //an integer array (param 1) and its size (param 2) //Output:    //True or false //Behavior:    //Returns true is there exists    //a sequence of 3 *consecutive* values in the array    //such that the sum of the first two elements    //is equal to the third...
20.14 Program BinarySearch Objectives Examine a binary search algorithm Debug existing code Instructions For this lab...
20.14 Program BinarySearch Objectives Examine a binary search algorithm Debug existing code Instructions For this lab you will code directly in ZyBooks. That means no uploading a file. If you wish, you can copy the template code into your IDE, work out a solution, and paste that into the code window. The problem The code does not work on certain data sets. Fix the sets but do not alter the binary search algorithm. The obvious Yes, this is a problem...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT