COMP1927 - Binary Search

Binary search is a searching algorithm, that locates an item in a sorted list by searching in the middle and then halving the range based on whether it returned higher or lower.

int binarySearch (int array[], int n, int key) {
 
   int found = FALSE;
   int start = 0;
   int  end = n-1;
   int mid;
 
   while ((start <= end) && (!found)) {
      mid = (start+end)/2;
      if (array[mid] == key) {
         found = TRUE;
      }
      else if (array[mid] < key) {
         start = mid + 1;
      }
      else { 
         end = mid - 1;
      }
   }
 
   return found;
}