binary search 2
#include <iostream>
using namespace std;
// function for binary search :
int Bsearch(int arr[], int size, int key ){
int start = 0;
int end = size - 1;
int mid = (start + end )/2;
while (start<=end)
{
if (arr[mid]==key)
{
return mid;
}
if (arr[mid]<key)
{
start = mid + 1;
}
else{
end = mid - 1;
}
mid = (start + end )/2;
}
return -1;
}
int main(){
int marray[7] = {22,33,44,55,66,77,88};
int answer = Bsearch(marray,7,55);
cout<<" the index is : "<<answer<<endl;
return 0;
}
Comments
Post a Comment