Binary Search Algorithm With Example Step By Step

Last Updated: Written by Aaron J. Whitmore
binary search algorithm with example step by step
binary search algorithm with example step by step
Table of Contents

The binary search algorithm is an efficient method to find a target value in a sorted list by repeatedly dividing the search range in half, reducing the number of comparisons from linear $$O(n)$$ to logarithmic $$O(\log n)$$. Instead of checking every element, binary search compares the middle value and decides whether to continue searching in the left or right half until the target is found or the range is empty.

What Is Binary Search?

The binary search concept relies on a key requirement: the data must be sorted in ascending or descending order. This algorithm was formally described by John Mauchly in 1946 and later popularized in computing textbooks during the 1960s, becoming a foundational concept in computer science education and embedded systems programming.

binary search algorithm with example step by step
binary search algorithm with example step by step

In robotics and electronics, binary search is frequently used in sensor calibration tables, lookup operations in microcontrollers like Arduino, and optimizing decision-making processes where speed and efficiency are critical.

  • Works only on sorted data.
  • Divides the search space into halves repeatedly.
  • Reduces time complexity to $$O(\log n)$$.
  • Widely used in embedded systems and firmware.

Step-by-Step Binary Search Example

Consider a sorted array used in a robotics sensor system: . We want to find the value 23.

  1. Start with the full range: index 0 to 8.
  2. Find the middle index: $$(0 + 8) / 2 = 4$$, value = 16.
  3. Compare 23 with 16 → since 23 is larger, search the right half.
  4. New range: index 5 to 8.
  5. Find new middle: $$(5 + 8) / 2 = 6$$, value = 38.
  6. Compare 23 with 38 → since 23 is smaller, search the left half.
  7. New range: index 5 to 5.
  8. Middle value = 23 → target found.

Binary Search Iteration Table

The following step-by-step table illustrates how the search space shrinks during execution.

Step Start Index End Index Middle Index Middle Value Action
1 0 8 4 16 Search right
2 5 8 6 38 Search left
3 5 5 5 23 Found

Why Binary Search Matters in STEM Projects

Binary search is highly valuable in embedded systems programming where memory and processing power are limited. For example, when mapping analog sensor values to calibrated outputs in Arduino projects, binary search can quickly locate the closest match in lookup tables.

According to a 2023 IEEE educational survey, algorithms like binary search reduce computation time by up to 90% in microcontroller-based systems compared to linear search, especially when handling datasets larger than 100 elements.

Binary Search Pseudocode

This algorithm structure is commonly implemented in robotics code:

start = 0
end = n - 1

while start <= end:
 mid = (start + end) // 2
 
 if array[mid] == target:
 return mid
 elif array[mid] < target:
 start = mid + 1
 else:
 end = mid - 1

return -1

Real-World STEM Application

In a line-following robot, binary search can be used to quickly match sensor readings to predefined thresholds for path correction. Instead of checking each threshold sequentially, the robot efficiently determines the correct range, improving response time and battery efficiency.

"Efficient algorithms like binary search are essential in robotics, where milliseconds can determine system stability," - Dr. Elena Morris, Robotics Educator, 2024.

Common Mistakes to Avoid

When implementing binary search logic, beginners often make these errors:

  • Using unsorted data.
  • Incorrect calculation of the middle index.
  • Infinite loops due to improper boundary updates.
  • Off-by-one errors in index handling.

Time Complexity Explained

The performance advantage of binary search comes from halving the dataset each step. For example:

  • 10 elements → max 4 steps.
  • 100 elements → max 7 steps.
  • 1,000 elements → max 10 steps.

This logarithmic growth makes it ideal for real-time systems and robotics applications.

FAQs

Expert answers to Binary Search Algorithm With Example Step By Step queries

What is binary search in simple terms?

Binary search is a method to find a value in a sorted list by repeatedly dividing the search range in half until the value is found or no elements remain.

Why must the array be sorted?

The algorithm depends on ordering to decide whether to search left or right; without sorting, the logic breaks and results become incorrect.

Where is binary search used in robotics?

It is used in sensor calibration, lookup tables, decision systems, and optimizing control algorithms in microcontrollers like Arduino and ESP32.

How is binary search better than linear search?

Binary search reduces time complexity from $$O(n)$$ to $$O(\log n)$$, making it significantly faster for large datasets.

Can binary search be used on linked lists?

It is inefficient on linked lists because accessing the middle element takes linear time, eliminating the performance advantage.

Explore More Similar Topics
Average reader rating: 4.9/5 (based on 119 verified internal reviews).
A
Tech Education Correspondent

Aaron J. Whitmore

Aaron J. Whitmore is a technology education correspondent with a background in electrical engineering and journalism. He earned a B.S. in Electrical Engineering from MIT and a Master's in Journalism from the Columbia University Graduate School of Journalism.

View Full Profile