What is the time complexity of binary search?

Practice Questions

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

Questions & Step-by-Step Solutions

What is the time complexity of binary search?
  • Step 1: Understand that binary search is used to find an item in a sorted list.
  • Step 2: Start with the entire list as your search interval.
  • Step 3: Check the middle item of the list.
  • Step 4: If the middle item is the one you're looking for, you're done.
  • Step 5: If the middle item is greater than the target, narrow your search to the left half of the list.
  • Step 6: If the middle item is less than the target, narrow your search to the right half of the list.
  • Step 7: Repeat steps 3 to 6 until you find the item or the interval is empty.
  • Step 8: Each time you narrow the search, you cut the number of items to check in half.
  • Step 9: This halving process leads to a logarithmic time complexity, which is expressed as O(log n).
No concepts available.
Soulshift Feedback ×

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

Not likely Very likely