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

Practice Questions

Q1
What is the time complexity of a binary search algorithm on a sorted array?
  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 a binary search algorithm on a sorted array?
  • Step 1: Understand that binary search is used on a sorted array.
  • Step 2: Start with the entire array as the 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, narrow the search to the left half. If it's greater, narrow it to the right half.
  • Step 7: Repeat steps 3 to 6 until you find the target or the search interval is empty.
  • Step 8: Each time you narrow the search interval, you are effectively dividing the number of elements to search by 2.
  • Step 9: This division by 2 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