Computer Programming || Gaming || Technology || ai

Binary search Algorithm


#include <bits/stdc++.h>
using namespace std;
int binarySearch(int array[],int size,int searchValue);


int main(){
    int a[]={12,22,34,47,55,67,82,98};
    int userValue;
    cout<<"Enter an integer : "<<endl;
    cin>>userValue;
int result=binarySearch(a,8,userValue);
if(result>=0){
cout<<"The number " <<a[result]<<"was found at the element with index"<<result<<endl;
}
else{
cout<<"The number "<<userValue<<"was not found"<<endl;
}
}

int binarySearch(int array[],int size,int searchValue){

  int low=0;
  int high=size-1;

  int mid;
  while(low<=high){
  mid=(low+high)/2;
    if(searchValue==array[mid]){
      return mid;
    }
    else if(searchValue>array[mid]){
      low=mid+1;
    }
    else
      high=mid-1;
    }
  return -1;
}

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *




Calender

October 2020
M T W T F S S
 1234
567891011
12131415161718
19202122232425
262728293031