- 2070 Bhadra · 2 marks
Complexity of binary search
Short answer
Worst and average case O(log₂ n), best case O(1). The array must be sorted. Space complexity O(1) for the iterative version.
What is the complexity of binary search?
Each comparison in binary search halves the number of remaining elements: n, n/2, n/4, ..., 1. The number of halvings is log₂ n.
- Worst and average case: O(log₂ n) (about ⌊log₂ n⌋ + 1 comparisons)
- Best case: O(1), when the key is at the first middle position
- Space: O(1) for the iterative version
For n = 1,000,000 at most about 20 comparisons are needed. The array must be sorted.
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Binary search with algorithm and example · Data Structure and Algorithm, chapter 8 · asked 4×
- Binary search for 54 in sorted data · Data Structure and Algorithm, chapter 8
- Binary search for 89; hash functions · Data Structure and Algorithm, chapter 8
- Sequential vs binary search · Data Structure and Algorithm, chapter 8
- BST properties and recursive search · Data Structure and Algorithm, chapter 6
- Binary tree traversals; preorder of BST · Data Structure and Algorithm, chapter 6
- M-ary error probability; binary vs M-ary · Communication Systems, chapter 9
- File stream; student binary file program · Object Oriented Programming, chapter 8
- Binary file of 48 students; highest mark · Computer Programming, chapter 10