What is the time complexity of binary search in the average case?

Practice Questions

Q1
What is the time complexity of binary search in the average case?
  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 average case?
  • Step 1: Understand what binary search is. It is a method used to find an item in a sorted list by repeatedly dividing the search interval in half.
  • Step 2: Start with a sorted list of items. For example, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].
  • Step 3: In the first step, check the middle item of the list. If it is the target item, you are done. If not, decide whether to search the left half or the right half based on whether the target is smaller or larger than the middle item.
  • Step 4: Each time you check the middle item, you eliminate half of the remaining items from consideration. This is the key to binary search.
  • Step 5: Continue this process of checking the middle and dividing the list until you find the target item or the list is empty.
  • Step 6: The number of times you can divide the list in half is logarithmic in relation to the number of items. This is expressed as log base 2 of n, where n is the number of items in the list.
  • Step 7: Therefore, in the average case, the time complexity of binary search 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