Wednesday, 28 June 2017

Binary Search Algorithm

int binarySearch(int arr[],int size, int item)
{
 int left, right, middle;
 left = 0;
 right = size-1;
 while(left<=right)
 {
    middle = ((left + right)/2);
    if(item == arr[middle])
     {
       return(middle);
     } 

    if(item > arr[middle])
     {
       left = middle+1;
     }
    else
     {
       right = middle-1;
     }
  }
 return(-1);
} 

No comments:

Post a Comment