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.
Write a function with the following prototype: int add_ok (int s, unsigned x, unsigned y); This...
Write a function with the following prototype: int add_ok (int s, unsigned x, unsigned y); This function should return 1 if arguments x and y can be added without causing overflow, otherwise return 0. If s is 0, x and y are unsigned numbers. If s is 1, x and y are treated as signed numbers. I tried this code but it doesn't work. Does anyone help me please? #include int add_ok(int s, unsigned x, unsigned y) { s=x+y; if...
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...
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...
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.
Please write code in c++ using iostream library. Write a function bool cmpr(char * s1, int...
Please write code in c++ using iostream library. Write a function bool cmpr(char * s1, int SIZE1, char * s2, int SIZE2) that compares two strings. Input Input contains two strings. Each string is on a separate line. example: aqua aqua Output Output YES if given two strings are the same or NO otherwise. YES
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT