What is the time complexity of binary search in the worst case scenario?

Practice Questions

Q1
What is the time complexity of binary search in the worst case scenario?
  1. O(1)
  2. O(log n)
  3. O(n)
  4. O(n log n)

Questions & Step-by-Step Solutions

What is the time complexity of binary search in the worst case scenario?
  • Step 1: Understand that binary search is used on a sorted list of items.
  • Step 2: Know that binary search works by repeatedly dividing the list in half.
  • Step 3: In each step, it checks the middle item of the current list segment.
  • Step 4: If the middle item is the target, the search is complete.
  • Step 5: If the target is less than the middle item, it continues searching in the left half.
  • Step 6: If the target is greater than the middle item, it continues searching in the right half.
  • Step 7: This halving process continues until the search space is reduced to one item.
  • Step 8: The number of times you can halve a list of size n is log base 2 of n.
  • Step 9: Therefore, in the worst case, binary search takes O(log n) time.
No concepts available.
Soulshift Feedback ×

On a scale of 0–10, how likely are you to recommend The Soulshift Academy?

Not likely Very likely