Time and Space Complexity of Binary Search

Binary Search is an efficient algorithm designed for searching within a sorted list of items. Its approach involves iteratively dividing the search space in half, until the target element found in the array.
The time complexity of the Binary Search Algorithm is , Where n is the size of the sorted linear array. It means the complexity grows logarithmically as the size of array increases and the space complexity of its algorithm is .
Time Complexity of Binary Search
Transform Your Career
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
Best Case Time Complexity of Binary Search
The best case scenario of Binary Search occurs when the target element is in the central index.In this situation, there is only one comparison. Therefore, the Best Case Time Complexity of Binary Search is O(1).
Average Case Time Complexity of Binary Search
The average case arises when the target element is present in some location other than the central index or extremities. The time complexity depends on the number of comparisons to reach the desired element.
In the following iterations, the size of the subarray is reduced using the result of the previous comparison. - Initial length of array - Iteration 1 - Length of array - Iteration 2 - Length of array - Iteration k - Length of array
After k iterations, the size of the array becomes 1 (narrowed down to the first element or last element only).
Length of array
=>
Applying log function on both sides:
=>
=>
=>
Therefore, the overall Average Case Time Complexity of Binary Search is O(logn).
Turn Learning into Career Growth
Worst Case Time Complexity of Binary Search
The worst-case scenario of Binary Search occurs when the target element is the smallest element or the largest element of the sorted array.
Since the target element is present in the extremitites (first or last index), there are logn comparisons in total. Therefore, the Worst Case Time Complexity of Binary Search is O(logn).
Scaler Placement Report and Statistics
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
Space Complexity of Binary Search
- In the case of the iterative approach, no extra space is used. Hence, the space complexity is O(1).
- In the worst case, logn recursive calls are stacked in the memory.
- i comparisons require i recursive calls to be stacked in memory.
- Since average time complexity analysis has logn comparisons, the average memory will be O(logn).
Thus, in recursive implementation the overall space complexity will be O(logn).
Conclusion
- Binary Search is used for finding the location of an element in a linear array.
- Binary search uses the divide and conquer technique.
- Learn What is the time complexity of binary search as follows:
- Best Case -
- Worst Case -
- Average Case -
- The space complexity for binary search is
- recursive approach -
- iterative approach -
- The equation T(n)= T(n/2)+1 is known as the recurrence relation for binary search.