Time and Space Complexity of Binary Search

Learn via video course
FREE
View all courses
DSA Problem Solving for Interviews using Java
DSA Problem Solving for Interviews using Java
by Jitender Punia
1000
4.9
Start Learning
DSA Problem Solving for Interviews using Java
DSA Problem Solving for Interviews using Java
by Jitender Punia
1000
4.9
Start Learning
Topics Covered

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 O(log2n)O(log_2{n}), 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 O(1)O(1).

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

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).

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 =n= n - Iteration 1 - Length of array =n/2= n/2 - Iteration 2 - Length of array =(n/2)/2=n/22= (n/2)/2 = n/2^2 - Iteration k - Length of array =n/2k= n/2^k

After k iterations, the size of the array becomes 1 (narrowed down to the first element or last element only).

Length of array =n/2k=1= n/2^k = 1
=> n=2kn = 2^k
Applying log function on both sides:
=> log2(n)=log2(2k)log_2{(n)} = log_2{(2^k)}
=> log2(n)=klog22=klog_2{(n)} = k * log_2{2} = k
=> k=log2(n)k = log_2(n)

Therefore, the overall Average Case Time Complexity of Binary Search is O(logn).

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
  • 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 - O(1)O(1)
    • Worst Case - O(logn)O(logn)
    • Average Case - O(logn)O(logn)
  • The space complexity for binary search is
    • recursive approach - O(logn)O(logn)
    • iterative approach - O(1)O(1)
  • The equation T(n)= T(n/2)+1 is known as the recurrence relation for binary search.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more