Question: What is the time complexity of binary search in a sorted array?
Options:
O(n)
O(log n)
O(n log n)
O(1)
Correct Answer: O(log n)
Solution:
Binary search has a time complexity of O(log n) because it divides the search interval in half with each step.
What is the time complexity of binary search in a sorted array?
Practice Questions
Q1
What is the time complexity of binary search in a sorted array?
O(n)
O(log n)
O(n log n)
O(1)
Questions & Step-by-Step Solutions
What is the time complexity of binary search in 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 search, you are cutting the number of elements to search in half.
Step 8: This halving process continues until you 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?