JavaScript Binary Search<!-- --> | <!-- -->Patrick Desjardins Blog
Patrick Desjardins Blog

# JavaScript Binary Search

Posted on: June 14, 2017

Previously we say the linear search which is having an asymptotic upper bound of `O(n)`. This time, we increase the speed with a binary sort of `O(log(n))`. The speed is increased at the cost of having the requirement of initial array to be sorted.

```1var arrayToSearch = [2, 6, 8, 12, 43, 78, 99, 134, 144, 156, 199, 256, 500];2
3console.log("Found at position :" + binarySearch(arrayToSearch, 2));4console.log("Found at position :" + binarySearch(arrayToSearch, 256));5console.log("Found at position :" + binarySearch(arrayToSearch, 500));6console.log("Found at position :" + binarySearch(arrayToSearch, 44444));7
8function binarySearch(arrayToSearch, valueToSearch) {9  var start = 0;10  var end = arrayToSearch.length - 1;11
12  while (start <= end) {13    var middle = Math.ceil((end + start) / 2);14    var middleValue = arrayToSearch[middle];15    if (middleValue === valueToSearch) {16      return middle;17    } else if (valueToSearch < middleValue) {18      end = middle - 1;19    } else {20      start = middle + 1;21    }22  }23  return -1;24}```

The idea is so split the array in 2, then look in which half the value may be. If the value is lower than the middle value, we divide reject the upper half. We keep doing it until we found the value or that the gap between the lower and upper bound doesn't exist.

In the case of finding 256, the lower bound start with the first index (value 2) and the `length-1` (value 500). The middle is index 6 (value 99). Since we are searching for 256 the value above 99, hence we reject all values on the left (index 0 to index 6 inclusively). To do so, we move the start to index `6+1` (value 134) and keep the end bound to `length-1` (500). And we iterate.

The first algorithm is using a while to loop through, but as we just described, we are doing over and over the same thing, just with a reduction of the array. It means that it's like asking for different array the same logic. This means that we can transform the iterative approach into a recursive approach.

```1var arrayToSearch = [2, 6, 8, 12, 43, 78, 99, 134, 144, 156, 199, 256, 500];2var start = 0;3var end = arrayToSearch.length - 1;4
5console.log("Found at position :" + binarySearch(arrayToSearch, 2, start, end));6console.log(7  "Found at position :" + binarySearch(arrayToSearch, 256, start, end)8);9console.log(10  "Found at position :" + binarySearch(arrayToSearch, 500, start, end)11);12console.log(13  "Found at position :" + binarySearch(arrayToSearch, 44444, start, end)14);15
16function binarySearch(arrayToSearch, valueToSearch, start, end) {17  if (start <= end) {18    var middle = Math.ceil((end + start) / 2);19    var middleValue = arrayToSearch[middle];20    if (middleValue === valueToSearch) {21      return middle;22    } else if (valueToSearch < middleValue) {23      end = middle - 1;24    } else {25      start = middle + 1;26    }27    return binarySearch(arrayToSearch, valueToSearch, start, end);28  }29  return -1;30}```