What is Binary search?

A search method for sorted data that compares the target with the middle element and halves the search range each time. It finds a value among n items in at most ⌊log₂ n⌋ + 1 comparisons, so its complexity is O(log n). The data must be sorted.

Japanese name: 二分探索

Binary search appears in 7 practice questions across 3 topics, including Data Structures and Algorithms, A/D and D/A conversion, Software Development and Project Management.

Field: Data Structures and Algorithms

Try it: questions about Binary search

Which is a prerequisite for using binary search, which compares the target with the middle element and halves the search range each time?

  1. AThe number of data items must be a power of two
  2. BThe data must be sorted by key
  3. CThe data must not contain duplicate values
Show answer and explanation

Answer: B. The data must be sorted by key

Binary search uses the result of comparing the target with the middle element to decide whether the target lies in the first or second half, and halves the range. That conclusion holds only because the data is sorted, so binary search cannot be used on an unsorted array. The number of items does not need to be a power of two; choosing the middle as (low + high) ÷ 2 rounded down works for any size. Duplicates are also fine: one of the matching values is found (finding the first occurrence takes a small extra step).

A binary search is performed on 1,000 distinct values sorted in ascending order. If one comparison tells you whether the values are equal, smaller or larger, what is the maximum number of comparisons, counting both successful and unsuccessful searches?

  1. A10
  2. B32
  3. C500
Show answer and explanation

Answer: A. 10

Each comparison roughly halves the search range, and 2¹⁰ = 1024 ≥ 1000, so the range is used up after at most 10 comparisons (⌊log₂ 1000⌋ + 1 = 10). The complexity is O(log n), and even 1,000,000 values need at most 20 comparisons. 32 corresponds to √1000 ≈ 31.6, and 500 is close to the average for a linear search (about n/2). Keep in mind, though, that binary search needs sorted data, and sorting itself costs O(n log n).

Binary search in the explanations

Questions that mention Binary search (7)

  1. Which is a prerequisite for using binary search, which compares the target with the middle element and halves the search range each time?
  2. A binary search is performed on 1,000 distinct values sorted in ascending order. If one comparison tells you whether the values are equal, smaller or larger, what is the maximum number of comparisons, counting both successful and unsuccessful searches?
  3. A binary search looks for 38 in the ascending array a[0] to a[9] = 3, 8, 12, 17, 23, 31, 38, 45, 52, 60. The search range starts with lo = 0 and hi = 9, and the middle position is (lo + hi) ÷ 2 rounded down. Which lists, in order, the elements compared with 38?
  4. After a firmware version known to work correctly there are 1,000 commits, and the latest commit has a bug. The bug was introduced by one commit and occurs in every commit after it. When the culprit commit is identified by binary search, as with git bisect, what is the maximum number of times the firmware must be built and tested?
  5. Which correctly describes the operation of a successive-approximation (SAR) ADC?
  6. How many comparisons does an N-bit successive-approximation ADC basically perform per conversion?
  7. A linear search compares the elements of an array of 100 distinct values one by one from the front to find a target. If the target is always in the array and is equally likely to be at any position, what is the average number of comparisons?

Books on Data Structures and Algorithms Ad

As an Amazon Associate I earn from qualifying purchases.

Semiconductor, Electronics and Computing Calculators

Related terms

Array Git Comparator Microcontroller (MCU) Oversampling Successive-approximation ADC Binary heap Binary search tree Breadth-first search Depth-first search

Back to the glossary