What is the worst-case time complexity of binary search on n elements?
- A O(n)
- B O(log n)
- C O(n log n)
- D O(1)
Explanation
Each comparison halves the search space, so at most log2 n comparisons are needed, giving O(log n) worst-case time.
What is the worst-case time complexity of binary search on n elements?
Each comparison halves the search space, so at most log2 n comparisons are needed, giving O(log n) worst-case time.