# Time & Space Complexity of Binary Search

Published
24th Jan, 2023
Views
20,380

Binary search is a fast way to find an item in a sorted list. This works by dividing the list in half until you have narrowed down the possible locations. The introductory tutorial used binary search to solve the guessing game.

A binary search can also be called a logarithmic or half-interval search. As a programmer, you can gain enough knowledge about binary search as well as binary treeand you can get expert guidance by opting for the advanced Programming course through KnowledgeHut.

## What is Binary Search Algorithm?

binary search algorithm, one of the most popular search techniques, is used. This algorithm can be used for sorting arrays. This search technique is based on the 'divide and conquers' strategy. In every iteration, the search space is reduced to half. Binary search algorithms locate a particular value within a sorted array. This search algorithm uses the principle of divide-and-conquer. However, it is very fast and requires the data to be sorted. The search begins in the middle of an array and then moves down to the lower or upper halves of the sequence. The search should go higher if the median value is lower than the target value. If not, it will need to focus on the descending part of the array.

Binary Search Algorithm can be a very efficient way to search, but it requires some order as to which part of the array it will appear.

Source

Applications of Binary Search

• This algorithm can search elements in a sorted array more efficiently.
• You could also use it to perform a few additional operations, such as finding an array's smallest or largest element.

• It follows the technique of eliminating half the array elements and is, therefore, more efficient than a linear search for large amounts of data.
•  Lower time complexity, which means it takes less time to compile.
• This is a better option than linear search because it splits the array in half rather than sequentially traversing through each element.

Limitations to Binary Search Algorithms

• The binary search algorithm can only be applied to a sorted array.
• It would be time-consuming to sort and search for the desired element in small unsorted arrays. Binary search is not recommended in these cases.
• It is less localized than a linear search algorithm for in-memory searches at short intervals.

## How Does Binary Search Work?

A binary search is a fast way to search an ordered list.

To implement an algorithm using a programming language, it is necessary to understand the algorithm fully. What are the inputs? What are the outputs? Which variables should be created, and what initial values? What intermediate steps should you take to calculate other values and finally compute the output? These steps should not be repeated if they can be simplified using a loop.

Binary search's main purpose is to track the range of reasonable guesses.

This is how a binary search works:

• Set the counter in the middle of the list.
• The search will end if the value found matches.
• The list will be divided in half if the value at its midpoint is lower than the value to be found. The search continues to the top half of the list and ignores the lower half.
• If the value at the middle point is greater than what is to be found, then the search will continue to the lower half.
• The search continues to the middle of all remaining items. Continue with steps 2 through 4.

### Examples Of Binary Searches

Binary search algorithms are often called "the algorithm that is everyday life." It's almost automatic! These are just a few examples of binary searches you might encounter daily.

1. Dictionaries

You are suddenly without internet access and need to search for the definition of "wombat" online. This means you should behave like your primitive ancestors and reach for a physical dictionary. You could do a linear search by starting at the "A" words and working through the dictionary to reach "waldo."

But most people are smarter than that and instinctively use the binary search method. We refer to the "W" listings and go to the middle section. We ignore any pages on the right if "waldo" alphabetically is smaller than the word on the middle page. If "waldo" is larger than the word on that middle page, we disregard the pages on the right-hand side. Then, we keep going until we find it.

2. Going To the Library

Another use involves the inability to access the Internet. To find a book called "Atomic Habits," you can visit your local library. If you linearly search the shelves, you will stay there forever. Instead, use alphabetization or a code system such as the Dewey Decimal System for a more precise search.

3. When to Use Binary Search

You will be astonished to discover that binary searches always happen in our daily lives. The use of binary searches is natural and frequent in daily life.

You can use binary search algorithms to find a single element in a sorted sequence. They do have a lot of additional purposes, though. For instance, a result can be subjected to a binary search.

Imagine that you needed to find the minimum amount of space required to accommodate all employees in an office. You can then conduct a binary search to find the suggested size rather than going through each dimension sequentially. You would typically estimate the maximum and minimum sizes for the binary search. Then, you'd check a middle value to see if you can reduce the interval by half until you find your answer. This saves time and is especially useful when considering the many possible office space variations per square foot.

You can also find other useful examples, such as code testing and exams, interviews for technical staff, code challenges, and tasks in the library.

4. Binary Search Implementation

There are two types: iterative or recursive binary search implementations. Recursive and Iterative methods have O(logN), whereas the Iterative Method uses O(1). While the recursive approach is easier to implement, the iterative is more efficient.

An iterative algorithm repeats the same statement set a certain number of times. The algorithm can be used to repeat the same steps multiple times by using looping statements (e.g., loop, while loop, or do-while loop).

Recursive algorithms, on the other hand, depend on a function calling itself repeatedly to reach its base condition (also called the stopping condition).

## Time and Space Complexity of Binary Search

Any physical object in the Universe is defined by space and time. Space and Time complexity can also determine the effectiveness of an algorithm. Although we all know that there are many ways to solve a programming problem, it is important to understand how an algorithm works well to improve the programming experience. Knowing how to assess the program/algorithm using Space and Time complexity is a great way to determine its effectiveness. This will allow programmers to be more efficient.

You can explore the complexity of binary search algorithms in the future through the KnowledgeHut Python Online Course. Let us instead focus on Time complexity. Time is money! A brief idea of time complexity in binary search and how to assess a program that is based on Time complexity.

### What is Time complexity?

The time complexity of the binary search is the time it takes to execute as a function of the input length. It measures how long it takes to execute each code statement in an algorithm. It won't examine the whole execution time of an algorithm. It will instead provide information about variations (increases or decreases) in execution time as a function of the number (increase) or decrease in an algorithm. The length of the input is the only determinant of how long it takes to complete the task.

### Source

This term, space complexity of the binary search, is a way to talk about time complexity. It refers to the storage space or workspace that an algorithm requires. It is directly proportional to how much input the algorithm receives. Space complexity can be calculated by simply calculating the amount of space used by variables in an algorithm. The algorithm will run faster if there is less space. It is important to remember that space complexity and time are unrelated.

The binary search algorithm finds a specific element in a list with O (log n complexity), where _n represents the total number of elements within that list. Binary search cannot be used on a sorted list. Binary search can only be used when elements are arranged in a particular order. Binary search cannot work with elements arranged in an alternate or random order.

The search starts by comparing the search results with the middle element in the list. The search result will be called "element found" if the search element matches. If the search element matches, the result will be "element found." If the search element is smaller than the middle element, we will continue the process with the sublist to its left. If the search element is large, we will continue the process for the right-hand list of middle elements. The process continues until the search element in question is found on the list or until there are no more elements. If the element does not match the search element, it will return "Element not found in the list".

These steps can be used to implement binary searches:

• Step I: The search element must be removed from the user.
• Step II: Find the middle element in the sorted list.
• Step III: Compare the search element to the middle element of the sorted listing.
• Step IV: Display "Given element located !!!"." if both elements match. Stop the function.
• Step V: If the elements do not match, verify that the search element in question is smaller or larger than the middle.
• Step VI: If the search element is smaller than the middle, repeat steps II, III, IV, and V for the left sublist of middle elements.
• Step VII: To find the correct sublist for the middle elements, you can repeat steps II, IV, and V if the search element is larger than the middle one.
• Step VIII: Continue this process until you locate the search element in the list or until your sublist contains only one element.
• Step IX: Display "Element not found" in the !!!". The list of elements does not match the search element. Stop the function.

### Recursive and Iterative Binary Search Algorithms

The binary search comes in two versions. The recursive version of Binary Search is O(log N), and the iterative version is O(1). Binary search is different because it has a different space complexity. Binary Search's iterative version is more efficient, even though it can be simpler to implement.

Binary search is a search technique that finds the value of a target or key within an array. Binary search compares the middle elements and target of an array. The target will be removed from the search results if they are different.

This search algorithm is based on the principle of Divide and Conquer. Binary Search works similarly to all Algorithms, which divide and conquer. It first splits the large array into smaller parts and then solves it Recursively or iteratively. The best algorithm for this task is if the data are in a sorted format. Binary search divides large arrays into smaller sub-arrays. Then iteratively solves the problem or recursively.

Binary search can be performed using either Iterative and Recursive algorithms. Both methods may be used for the same task.

### Iterative approach

```binarySearch(arr, size)
loop until beg is not equal to end
midIndex = (beg + end)/2
if (item == arr[midIndex] )
return midIndex
else if (item > arr[midIndex] )
beg = midIndex + 1
else
end = midIndex - 1
Recursive approach H3
binarySearch(arr, item, beg, end)
if beg<=end
midIndex = (beg + end) / 2
if item == arr[midIndex]
return midIndex
else if item < arr[midIndex]
return binarySearch(arr, item, midIndex + 1, end)
else
return binarySearch(arr, item, beg, midIndex - 1)
return -1 ```

Binary search is at its best when:

• The middle of the list is where you should search for the element.
• This is because the element is located in the first step, and it involves 1 comparison.
• Thus, Binary Search has O(1) as the Best Case Time Complexity.

Let there be N distinct numbers: a1, a2, ..., a(N-1), aN

We must find element P.

Two cases are possible:

• Case 1: The element of P can be found in N distinct indexes, ranging from 0 to 1.
• Case 2: In some cases, the element P may not be on the list.

There are N cases 1 and N cases 2. There are N+1 cases in total to be considered.

Binary Search will perform K+1 comparisons if element P is found in index K.

It's because:

The element at index N/2 can be found in 1 comparison as Binary Search starts from the middle.

Similarly, in the 2nd comparison, elements at index N/4 and 3N/4 are compared based on the result of the 1st comparison.

On this line, in the 3rd comparison, elements at index N/8, 3N/8, 5N/8, and 7N/8 are compared based on the result of the 2nd comparison.

Based on this, we know that:

• Elements requiring 1 comparison: 1
• Elements requiring 2 comparisons: 2
• Elements requiring 3 comparisons: 4

Therefore, Elements requiring I comparisons: 2^(I-1)

The maximum number of comparisons = Number of times N is divided by 2 so that result is 1 = Comparisons to reach 1st element = logN comparisons

I can vary from 0 to logN

• Total number of comparisons = 1 * (Elements requiring 1 comparison) + 2 * (Elements requiring 2 comparisons) + ... + logN * (Elements requiring logN comparisons)
• Total number of comparisons = 1 * (1) + 2 * (2) + 3 * (4) + ... + logN * (2^(logN-1))
• Total number of comparisons = 1 + 4 + 12 + 32 + ... = 2^logN * (logN - 1) + 1
• Total number of comparisons = N * (logN - 1) + 1
• Total number of cases = N+1
• Therefore, average number of comparisons = ( N * (logN - 1) + 1 ) / (N+1)
• Average number of comparisons = N * logN / (N+1) - N/(N+1) + 1/(N+1)
• The dominant term is N * logN / (N+1), which is approximately logN. Therefore, the Average Case Time Complexity of Binary Search is O(logN).

Worst case complexity of the binary search is considered when:

• Search for the element you are looking for in either the first or last index
• In this instance, the number of required comparisons is logN.
• Binary Search has O(logN) as the Worst Case Time Complexity.
1. Binary Search will be an O(logN space complex in a recursive implementation.
2. Binary Search will be executed iteratively so that the space complexity is O(1).
3. Two variables are required to keep track of the number of elements that need to be checked. Additional data is not necessary.
4. In the worst-case scenario, there will be a logN amount of recursive callings, and all these calls are stored in memory. Recursive calls will be stored in memory if there are any comparisons. Our average-case complexity analysis shows that O(logN), which is the average memory, will likewise be stacked into memory.

## Benefits

A binary search algorithm has many benefits:

• Using each comparison eliminates half of the list that is not needed for further searches.
• This indicates whether the element to be searched is located before or after the current position within the list.
• This information can be used to limit your search.
• It works much better than linear searches for large data sets.

## Conclusion

Binary search algorithms dominate programming. It is a good idea to study binary search algorithms, binary search best and worst case, and other related topics if you are interested in machine learning and data science; also, you can opt to learn Data Science courses online through KnowledgeHut. This topic will require practical knowledge as well as theoretical knowledge. We have introduced the concepts of the time complexity of binary search in this blog and explained why it is important to be included in any algorithm we create. We also learned about the various types of time complexity used for different kinds of functions.

Finally, we learned how to assign the order in which an algorithm is notated based on the cost function and the number of statements to be run. Knowing the time complexity of an algorithm for a given input size can help us plan our resources and process the results efficiently. Knowing the time complexity and the binary search tree time complexity of your algorithm can help you to do this and make you a more effective programmer to deal with any kind of complexity of the binary search algorithm.

#### Spandita Hati

Blog Author

Spandita is a dynamic content writer who holds a master's degree in Forensics but loves to play with words and dabble in digital marketing. Being an avid travel blogger, she values engaging content that attracts, educates and inspires. With extensive experience in SEO tools and technologies, her writing interests are as varied as the articles themselves. In her leisure, she consumes web content and books in equal measure.

Want to learn the top programming languages?

Avail your free 1:1 mentorship session.

Select

1What is the time complexity of the search?

Binary search algorithms are O(log n) in time complexity. O(1) would be the best-case complexity when the central index matches the desired value.

2Which is the best time complexity?

O(n) is the best-case complexity for insertion sorts. O(n) complexity is often referred to as linear complexity.

3What is time complexity in data structure?

Time complexity refers to the computational complexity of an algorithm. It describes how long it takes to execute an algorithm. Time complexity is the time taken to execute each statement. It is, therefore, highly dependent upon the data size.

4Why is binary search better than linear?

Binary search has one major advantage: it doesn't scan every element in the list. It does not scan every element but only the first half of the list. The binary search is faster than a linear search.

#### Upcoming Programming Batches & Dates

NameDateFeeKnow more