binary search algorithm in python , and php

Binary search algorithm

Binary search is used to find if a sorted iterable , that can be indexed , contains an element in O(nlog(n)). It is faster than a a sequential search , where we can check if an element exists in a sorted iterable in O(n).

The binary search algorithm consists of :

  • finding the middle of the iterable
  • comparing the value that we want to search for , with the value located at the middle .
  • if The value we are searching for , is equal to the value located at the middle of the iterable , we have found the element , so we stop the search , and we return true .
  • if the value we are searching for , is larger than the value located at the middle of the iterable , we must search the middle + one of the iterable till its end , as this value will be located in the upper half .
  • if the value we are searching for , is smaller than the value located at the middle of the iterable , we must search from the start of the iterable , to the middle – one , as the value will be located in the lower half.
  • if the length of the iterable is zero , then the value doesn’t exist , and return false

Binary search in python

  1. def binarySearch(iter_get_len, searchElement):
  2.     ''' find search element in an iterable that has __get__  and __len__

  3.     '''
  4.     if len(iter_get_len) == 0:
  5.         # searchElement Doesn't exist
  6.         return False
  7.     lenOverTwo = len(iter_get_len)//2
  8.     if iter_get_len[lenOverTwo] == searchElement:
  9.         # searchElement found
  10.         return True
  11.     # searchElement  in the lower half
  12.     if searchElement < iter_get_len[lenOverTwo]:
  13.         return binarySearch(iter_get_len[0: lenOverTwo], searchElement)
  14.     # searchElement  in the upper half
  15.     if searchElement > iter_get_len[lenOverTwo]:
  16.         return binarySearch(iter_get_len[lenOverTwo + 1:], searchElement)


  17. binarySearch([1, 2, 3, 4, 5], 1) == True
  18. binarySearch([1, 2, 3, 4, 5], 2) == True
  19. binarySearch([1, 2, 3, 4, 5], 7) == False

Binary search in php

  1. function  binarySearch($array, $searchElement)
  2. {
  3. /*
  4.     @param  $array  
  5.     @param $searchElement  
  6.     @perform  search using binary search for the  $searchElement inside the  $array
  7. */
  8.     $lengthOfArray = count($array);
  9.     if ($lengthOfArray === 0) {
  10.         return false;
  11.     }
  12.     $lengthOfArrayOverTwo = (int)  ($lengthOfArray / 2);
  13.     if ($array[$lengthOfArrayOverTwo] == $searchElement) {
  14.         # searchElement found
  15.         return True;
  16.     }

  17.     if ($searchElement < $array[$lengthOfArrayOverTwo]) {
  18.         # search the lower half
  19.         return  binarySearch(array_slice($array, 0, $lengthOfArrayOverTwo), $searchElement);
  20.     }

  21.     if ($searchElement > $array[$lengthOfArrayOverTwo]) {
  22.         # searchElement the upper half
  23.         return  binarySearch(array_slice($array, $lengthOfArrayOverTwo + 1), $searchElement);
  24.     }
  25. }

  26. assert(binarySearch([1, 2, 3, 4, 5], 6) == False);
  27. assert(binarySearch([1, 2, 3, 4], 1) == True);