What is the time complexity of the binary search algorithm?

Practice Questions

Q1
What is the time complexity of the binary search algorithm?
  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 the binary search algorithm?
  • Step 1: Understand what binary search does. It looks for a specific value in a sorted list by repeatedly dividing the list in half.
  • Step 2: Start with the entire list. Check the middle element to see if it is the value you are looking for.
  • Step 3: If the middle element is not the target value, decide whether to search the left half or the right half of the list based on whether the target value is smaller or larger than the middle element.
  • Step 4: Repeat the process with the chosen half of the list. Each time you do this, you are cutting the number of elements you need to search in half.
  • Step 5: Continue this process until you either find the target value or the list is empty.
  • Step 6: Since you are halving the number of elements to search each time, the number of steps it takes to find the value grows logarithmically with the size of the list, which is why the time complexity is 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