Binary Search is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(Log n).
3. INTRODUCTION
Binary Search is the search technique that works efficiently on sorted lists. Hence, to
search an element into some list using the binary search technique, we must ensure
that the list is sorted.
Binary search follows the divide and conquer approach in which the list is divided into
two halves, and the item is compared with the middle element of the list. If the match
is found then, the location of the middle element is returned. Otherwise, we search
into either of the halves depending upon the result produced through the match.
Binary Search 3
4. Binary Search is also known as half-interval search or logarithmic search, is a search
algorithm that finds the position of target value within a sorted array.
Binary Search runs in at worst logarithmic time, making {O(log n)} comparisons,
where {n} is the number of elements in the array and {log} is the binary algorithm; and
using only constant {(O(1))} space.
Binary Search 4
5. Difference between Linear Search and Binary Search –
Binary Search 5
Basis of Comparison Linear Search Binary Search
Definition The linear search starts
searching from the first
element and compares each
element with a searched
element till the element is not
found.
It finds the position of the
searched element by finding the
middle element of the array.
Approach It is based on the sequential
approach.
It is based on the divide and
conquer approach.
Size It is preferrable for the small-
sized data sets.
It is preferrable for the large-size
data sets.
Efficiency It is less efficient in the case
of large-size data sets.
It is more efficient in the case of
large-size data sets.
Sorted data In a linear search, the
elements don't need to be
arranged in sorted order.
The pre-condition for the binary
search is that the elements must
be arranged in a sorted order.
6. ALGORITHM
There are two methods to implement the binary search algorithm –
Iterative Method:
o If the target value is equal to the middle element, its position in the array is
returned.
o If the target value is less than the middle element, then we continue the
search in the lower half of the array.
o If the target value is greater than the middle element, then we continue
the search in the upper half of the array.
Recursive Method:
o Recursive implementation of binary search algorithm, in the method
binarySearch(), follows almost the same logic as iterative version, except
for a couple of differences.
Binary Search 6
7. Now, let’s see the algorithm of binary search –
Binary_Search(a, lower_bound, upper_bound, val)
Step 1: set start = lower_bound, end = upper_bound, pos = - 1
Step 2: repeat steps 3 and 4 while start <=end
Step 3: set mid = (start + end)/2
Step 4: if a[mid] = val
set pos = mid
print pos
Binary Search 7
8. go to step 6
else if a[mid] > val
set end = mid - 1
else
set start = mid + 1
[end of if]
[end of loop]
Step 5: if pos = -1
print "value is not present in the array"
[end of if]
Step 6: exit
Binary Search 8
9. EXAMPLE
Let the elements of array are –
Let the element to search is, K=56
We have to use the below formula to calculate the mid of the array –
mid=(start+end)/2
So, in the given array –
start = 0, end = 8 and mid = (0+8)/2 = 4
So, 4 is the mid of the array.
Binary Search 9
11. Now, the element to search is found. So algorithm will return the index of the element
matched.
Binary Search 11
12. TIME COMPLEXITY
Now, let's see the time complexity of Binary search in the best case, average case, and
worst case.
1. Best Case –
The element to be search is in the middle of the list.
In this case, the element is found in the first step itself and this involves 1
comparison.
Therefore, the Best Case Time Complexity of Binary Search is O(1).
Binary Search 12
13. 2. Average Case –
The average case time complexity of Binary search is O(log n).
3. Worst Case –
The element is to search is in the first index or last index.
In this case, the total number of comparisons required is {log n} comparisons.
Therefore, the worst case time complexity of Binary Search is O(log n).
Binary Search 13
14. Time Complexity:
Space Complexity
The Space Complexity of Binary Search is O(1).
Binary Search 14
Case Time Complexity
Best Case O(1)
Average Case O(log n)
Worst Case O(log n)
Space Complexity O(1)
15. ADVANTAGES & DISADVANTAGES
Advantages of Binary Search:
It is better than a linear search algorithm since its run time complexity is O(log
n).
At each iteration, the binary search algorithm eliminates half of the list and
significantly reduces the search space.
The binary search algorithm works even when the array is rotated by some
position and finds the target element.
Binary Search 15
16. Disadvantages of Binary Search:
The recursive method uses stack space.
Binary search is error-prone. Some of the common errors are as follows:
Off-by-one errors: While determining the boundary of the next interval, there
might be overlapping errors.
o Handling of duplicate items: While returning the first item, it might be
possible we return a subsequence similar item.
o Numerical underflows/overflows: In huge arrays when computing indices.
There might be overflows
o Recursive vs non-recursive implementation, which should be considered
while designing as recursive takes stack space.
Binary Search 16
17. APPLICATIONS
The applications of Binary Search are:
1. Find an element in a sorted array
2. Applications of Binary Search beyond arrays
To find if n is a square of an integer
Find the first value greater than or equal to x in a given array of sorted
integers
Find the frequency of a given target value in an array of integers
Find the peak of an array which increases and then decreases
A sorted array is rotated n times. Search for a target value in the array
Binary Search 17
18. 3. Real life applications of Binary Search –
Dictionary
Debugging a linear piece of code
Figuring out resource requirements
for a large system
Find values in sorted collection
Semiconductor test programs
Numerical solutions to an equation
18
Binary Search