Open In App

Why is Binary Search log n?

Last Updated : 19 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Binary search is a popular algorithm used to search for an element in a sorted list efficiently. In this article we can going to understand why binary search has a time complexity of O(log2 n).

How Binary Search Works?

Binary search works by repeatedly dividing the search interval in half. It starts by comparing the target value with the middle element of the array. If the target value matches the middle element, the search is successful. If not, it narrows down the search to either the left or right half of the array, based on the comparison result.

Halving the Search Space:

One key reason binary search is so efficient is that it halves the search space in each step. By eliminating half of the elements at each iteration, binary search greatly reduces the number of elements that need to be checked.

Logarithmic Time Complexity:

The reason binary search has a time complexity of O(log2 n) is because it divides the input size by 2 in each step. This halving process can be represented by a logarithmic function. In a list of size n, binary search can find an element in at most logâ‚‚(n) steps.

Comparing with Linear Search:

To understand the efficiency of binary search better, let’s compare it with linear search. In linear search, each element in the list is checked one by one until the target element is found. This results in a time complexity of O(n), where n is the number of elements in the list.

Conclusion:

In conclusion, binary search’s time complexity of O(log2 n) makes it a highly efficient algorithm for searching in sorted lists. By halving the search space at each step, binary search can quickly locate elements with significantly fewer comparisons compared to linear search.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads