top of page
  • Writer's pictureMarko Reljić

Demystifying Binary Search

Have you ever frantically searched a giant bookshelf for a specific book, only to find it in seconds later? Computers face a similar challenge when sifting through massive datasets. While a linear search (checking each item one by one) might work for small collections, it becomes agonizingly slow for larger ones. This is where binary search comes in, a powerful technique that cuts through the clutter with laser focus.

The Magic of Duality: Divide and Conquer

Binary search is a search algorithm that works exclusively on sorted arrays. Imagine yourself in a massive maze. Binary search doesn't check every corridor; instead, it exploits the sorted nature of the maze. Here's the magic:

  1. Start at the Center: The algorithm first zooms in on the element in the middle of the array.

  2. Is it a Match? It compares this element with the target value you're looking for.

  • Bingo! If they match, you've found your target, and the search ends.

  • Too High?  If the middle element is greater than the target, we know the target must be in the first half of the array (because the array is sorted). We can discard the second half entirely!

  • Too Low? Conversely, if the middle element is smaller than the target, the target must be in the second half of the array. We can eliminate the first half from consideration.

The Code Whisperer: Unveiling the Binary Search Logic

function binarySearch(arr: number[], t: number) {

  let low = 0;
  let high = arr.length - 1;

  while (low <= high) {
    let mid = Math.floor((low + high) / 2);

    if (arr[mid] === t) return mid;

    if (arr[mid] > t) {
      high = mid - 1;
    } else {
      low = mid + 1;
  return -1;
  • Initialization: We set two variables, low and high, to mark the beginning and end of the search space, which is initially the entire array.

  • The Looping Quest: The while loop continues as long as low is less than or equal to high. This ensures we have a valid search space.

  • The Oracle's Hint: The Midpoint: We calculate the mid index using integer division (Math.floor) to avoid potential overflow issues. This effectively divides the search space in half.

  • Truth or Dare? Comparisons: The code then compares the element at arr[mid] with the target value t:

  • Found the Treasure!  If they are equal, we've found our target and return the mid index.

  • Narrowing Down the Maze: If arr[mid] is greater than t, we know the target can only be in the first half. We update high to mid - 1, essentially discarding the right half.

  • Following the Right Path:  If arr[mid] is smaller than t, the target must be in the second half. We update low to mid + 1, discarding the first half.

  • Mission Incomplete: If the loop finishes without finding a match, it returns -1, indicating the target isn't present in the array.

The Beauty of Efficiency

The key to binary search's efficiency lies in its ability to repeatedly halve the search space with each comparison. This dramatically reduces the number of comparisons needed to find the target element. Mathematically, binary search boasts a time complexity of O(log n), where 'n' is the number of elements in the array. This means the search time grows logarithmically with the data size, making it significantly faster than linear search (O(n)) for large datasets.

Binary Search in Action

Binary search finds applications in various scenarios:

  • Information Retrieval: Search engines use binary search to quickly locate relevant web pages based on keywords.

  • In-Memory Databases:  Databases leverage binary search to efficiently find records within sorted indexes.

  • Machine Learning:  Binary search is used in algorithms like decision trees to classify data points.

By mastering binary search, you gain a powerful tool to navigate the ever-growing landscape of data. So, the next time you need to find something in a sorted collection, remember the magic of binary search – it will get you there swiftly and efficiently!

47 views0 comments

Recent Posts

See All


bottom of page