Understanding Binary Search: Data Structure and Algorithm

Binary search is a searching algorithm that finds the position of a target value within a sorted array. It works by repeatedly dividing the search interval in half. If the value of the search key is less than the item in the middle of the interval, the algorithm continues the search on the lower half.

Algogenz logo

9m · 5min read

Binary search is a fundamental algorithm in computer science that operates on sorted arrays or lists. It is a divide-and-conquer strategy that significantly reduces the time complexity compared to linear search methods, making it highly efficient for large datasets.


What Is Binary Search?

Binary search is a searching algorithm that finds the position of a target value within a sorted array.

It works by repeatedly dividing the search interval in half. If the value of the search key is less than the item in the middle of the interval, the algorithm continues the search on the lower half. Otherwise, it continues on the upper half. This process continues until the value is found or the interval is empty.


Scenario to Understand Binary Search

To grasp binary search, let's consider a simple analogy. Imagine flipping through a phone book to find a specific company's phone number. The pages are organized alphabetically, and instead of starting from the first page and reading each entry until you find the one you need, you could use a more intelligent strategy:

  1. Open the middle of the phone book.
  2. If the company you're looking for is listed after the middle page, discard the lower half of the book.
  3. If it's listed before, discard the upper half.
  4. Repeat steps 1-3 on the remaining half of the book.

This process is similar to binary search, where you continually halve the search space until you find the target or determine that it's not present.


How Does Binary Search Work?

Here's a step-by-step breakdown of how binary search works:

  1. Define the Search Interval: Start with the entire array or list as the search interval.
  2. Calculate the Midpoint: Divide the search interval into two halves by finding the midpoint.
  3. Compare the Middle Element: Check if the target value matches the middle element of the current search interval.
  4. Narrow Down the Search Space:
  • If the target matches the middle element, the search is complete, and the index of the value is returned.
  • If the target is less than the middle element, continue the search on the left half of the current interval.
  • If the target is greater than the middle element, continue the search on the right half of the current interval.
  • Iterate Until Found or Exhausted: Repeat steps 2–4 until the target value is found or the search interval is empty.


Implementing Binary Search

The implementation of binary search varies slightly depending on the programming language being used. Some languages provide built-in functions for binary search, which can simplify the task considerably. For example, Python has the bisect module that offers functions like bisect_leftbisect_right, and bisect for searching and inserting elements into sorted lists.


The Binary Search Algorithm can be implemented in two ways

  • Iterative Binary Search Algorithm
  • Recursive Binary Search Algorithm


Iterative Approach

An iterative implementation of binary search uses a while loop to keep reducing the search space.

Here's how it looks in pseudocode:

function binarySearch(arr, item)
  low = 0
  high = arr.length1
  while low <= high
    midIndex = (low + high) / 2
    if item == arr[midIndex]
      return midIndex
    else if item > arr[midIndex]
      low = midIndex + 1
    else
      high = midIndex - 1
  return -1

Here is the breakdown for this function:

  • arr is the sorted array to search.
  • item is the target value to find.
  • low and high are the start and end indices of the search interval.
  • midIndex is the index of the middle element of the current search interval.
  • The loop continues until low is greater than high, which means the search interval is empty.
  • If the target item is found at index midIndex, the index is returned.
  • If item is greater than the middle element, the search continues on the right half of the array (low = midIndex + 1).
  • If item is less than the middle element, the search continues on the left half of the array (high = midIndex - 1).
  • If the item is not found, the function returns -1.


Recursive Approach

A recursive version of binary search breaks down the problem into smaller subproblems by calling itself with a reduced interval. Here's the recursive pseudocode:

function binarySearch(arr, item, low, high)
  if low <= high
    midIndex = (low + high) / 2
    if item == arr[midIndex]
      return midIndex
    else if item < arr[midIndex]
      return binarySearch(arr, item, low, midIndex - 1)
    else
      return binarySearch(arr, item, midIndex + 1, high)
  return -1

Here is the breakdown:

  • arr is the sorted array to search.
  • item is the target value to find.
  • low and high are the start and end indices of the search interval.
  • midIndex is the index of the middle element of the current search interval.
  • The base case for the recursion is when low is greater than high, which means the search interval is empty and the function returns -1.
  • If the target item is found at index midIndex, the index is returned.
  • If item is less than the middle element, the function calls itself with the left half of the array (low to midIndex - 1).
  • If item is greater than the middle element, the function calls itself with the right half of the array (midIndex + 1 to high).


When to Use Binary Search

Binary search should be used when the data is sorted, as it relies on repeatedly dividing the search space in half. It's essential for applications like database indexing, file system searches, and many other areas where speed is crucial.


Advantages of Binary Search

  • Efficiency: Binary search has a logarithmic time complexity, which makes it very efficient even for large datasets.
  • Optimization: It reduces the search space by half in each iteration, leading to fewer comparisons overall. 
  • Usability: It is applicable to any sorted dataset, whether stored in arrays, linked lists, or other data structures.


Time Complexity and Efficiency

  • One of the main advantages of binary search over linear search (which scans through the entire list) is its efficiency. Binary search has a time complexity of O(log n), where n is the number of elements in the list. This makes it significantly faster for large datasets, especially when compared to linear search, which has a time complexity of O(n)


Practical Applications

Binary search is widely used in practical applications such as:

  • Database Systems: To quickly locate records.
  • File Systems: For finding files in directories.
  • Compiler Design: In lexical analysis to match keywords.
  • Digital Electronics: In digital search machines.


Binary Search Trees

While binary search is typically applied to simple arrays, it forms the basis of binary search trees (BSTs), which are a type of self-balancing tree data structure. BSTs are used extensively in computer science, particularly in database management systems. They maintain an ordered collection of items, allowing for fast lookup, addition, and removal operations


Conclusion

Binary search is a powerful algorithm that stands as a cornerstone in the field of data structures and algorithms. Its ability to efficiently locate elements within a sorted list makes it indispensable in various applications where quick retrieval is critical. As a developer, understanding binary search and knowing how to implement it effectively can significantly enhance the performance of your software.