What is the time complexity of performing a binary search on a sorted array?

Practice Questions

Q1
What is the time complexity of performing a binary search on a sorted array?
  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 performing a binary search on a sorted array?
  • Step 1: Understand that binary search is used on a sorted array.
  • Step 2: Start with the entire array as your search interval.
  • Step 3: Find the middle element of the current search interval.
  • Step 4: Compare the middle element with the target value you are searching for.
  • Step 5: If the middle element is equal to the target, you found it! If not, decide which half of the array to search next.
  • Step 6: If the target is less than the middle element, repeat the search on the left half of the array. If it's greater, repeat on the right half.
  • Step 7: Each time you repeat the process, you are cutting the number of elements to search in half.
  • Step 8: This halving continues until you either find the target or the search interval is empty.
  • Step 9: The number of times you can halve the array is logarithmic in relation to the number of elements, 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