Binary Search Concept
Code
Complexity Analysis
Searching is an important operation in every-day life and even in many software applications, as we frequently search for data!
Searching is a method to find some relevant information in a data set
Lets try to search for Number 12 in an array.
Lets try to search for Number 12 in an array.
Lets try to search for Number 12 in an array.
Lets try to search for Number 12 in an array.
Lets try to search for Number 12 in an array.
Lets try to search for Number 12 in an array.
Lets try to search for Number 12 in an array.
Lets try to search for Number 12 in an array.
and there is a Match at index 5!
This algorithm of linearly searching through the array is known as
Linear Search!
Binary Search is an algorithm that can be used to search an element in a sorted dataset.
By sorted, we mean that the elements will either be in a natural increasing or decreasing order.
10, 20, 28 , 56, 100
(Sorted Array)
Lets try to search for Number 12 in an array.
Initialise
s = 0, e = 8
Lets try to search for Number 12 in an array.
Find the midpoint
s = 0, e = 8
mid = (s+e)/2
= (0+8)/2
= 4
mid
Lets try to search for Number 12 in an array.
mid
Make a comparison between
arr[mid] & key
12
9 <
mid
9 <
Clearly
So, the we discard the part of the array from s to mid.
12
s = mid + 1
Hence, the new start becomes 5.
mid
mid
mid = (s + e)/2
= (5 + 8)/2
= 6
mid
Now again, we compare a[mid] and key
22 >
12
mid
a[mid] >
key
e = mid - 1
mid = (5 + 5)/2
= 5
mid
mid
a[mid] ==
12
mid
a[mid] ==
return mid;
12
mid
and we got the answer that the final index of 12 is actually 5.
Lets try to search for Number 6 in an array.
Lets try to search for Number 6 in an array.
mid
mid = (0 + 8)/2 = 4
Lets try to search for Number 6 in an array.
mid
9 >
Search in the left of mid
6
Lets try to search for Number 6 in an array.
mid
e = mid - 1
Lets try to search for Number 6 in an array.
mid = (0 + 3)/2 = 1
mid
Lets try to search for Number 6 in an array.
mid
Search in the right of mid
6
3 <
Lets try to search for Number 6 in an array.
mid
Search in the right of mid
6
3 <
Lets try to search for Number 6 in an array.
mid
s = mid + 1
6
3 <
Lets try to search for Number 6 in an array.
s = mid + 1
6
3 <
mid
Lets try to search for Number 6 in an array.
mid = (2 + 3)/2 = 2
mid
Lets try to search for Number 6 in an array.
Compare
mid
4 <
6
Lets try to search for Number 6 in an array.
Go Right, s = mid + 1
mid
4 <
6
Lets try to search for Number 6 in an array.
Go Right, s = mid + 1
mid
4 <
6
Lets try to search for Number 6 in an array.
mid
mid = (3 + 3)/2 = 3
Lets try to search for Number 6 in an array.
mid
Go right, s = mid + 1
5 <
6
Lets try to search for Number 6 in an array.
mid
Go right, s = mid + 1
5 <
6
Lets try to search for Number 6 in an array.
start becomes greater than end! 🧐
mid
Lets try to search for Number 6 in an array.
At this point, s > e, and it means we have covered the entire array but the element was not present,
and we STOP our search at this point.
Binary Search is a Divide & Conquer Algorithm.
It divides the array into two halves and tries to find the element K in one or the other half, not both. It keeps on doing this until we either find the element or the array is exhausted.
private static int binarySearch(int[] array, int K) {
}
private static int binarySearch(int[] array, int K) {
int n = array.length;
int s = 0;
int e = n - 1;
}
private static int binarySearch(int[] array, int K) {
int n = array.length;
int s = 0;
int e = n - 1;
while (s <= e) {
// Compute Mid
int mid = (s + e)/2;
}
}
private static int binarySearch(int[] array, int K) {
int n = array.length;
int s = 0;
int e = n - 1;
while (s <= e) {
// Compute Mid
int mid = (s + e)/2;
// Go Right
if (array[mid] < K) {
s = mid + 1;
}
}
}
private static int binarySearch(int[] array, int K) {
int n = array.length;
int s = 0;
int e = n - 1;
while (s <= e) {
// Compute Mid
int mid = (s + e)/2;
// Go Right
if (array[mid] < K) {
s = mid + 1;
}
// Go Left
else if (array[mid] > K) {
e = mid - 1;
}
}
}
private static int binarySearch(int[] array, int K) {
int n = array.length;
int s = 0;
int e = n - 1;
while (s <= e) {
// Compute Mid
int mid = (s + e)/2;
// Go Right
if (array[mid] < K) {
s = mid + 1;
}
// Go Left
else if (array[mid] > K) {
e = mid - 1;
}
// Equal!
else {
return mid;
}
}
}
private static int binarySearch(int[] array, int K) {
int n = array.length;
int s = 0;
int e = n - 1;
while (s <= e) {
// Compute Mid
int mid = (s + e)/2;
// Go Right
if (array[mid] < K) {
s = mid + 1;
}
// Go Left
else if (array[mid] > K) {
e = mid - 1;
}
// Equal!
else {
return mid;
}
}
return -1;
}
Let's discuss space and time
Let's dicuss the worst complexity
Iteration , Array Size
1 N/2
2 N/4
3 N/8
.
.
Stop
private static int binarySearch(int[] array, int K) {
int n = array.length;
int s = 0;
int e = n - 1;
while (s <= e) {
// Compute Mid
int mid = (s + e)/2;
// Go Right
if (array[mid] < K) {
s = mid + 1;
}
// Go Left
else if (array[mid] > K) {
e = mid - 1;
}
// Equal!
else {
return mid;
}
}
return -1;
}
Let's try to generalise this behaviour
private static int binarySearch(int[] array, int K) {
int n = array.length;
int s = 0;
int e = n - 1;
while (s <= e) {
// Compute Mid
int mid = (s + e)/2;
// Go Right
if (array[mid] < K) {
s = mid + 1;
}
// Go Left
else if (array[mid] > K) {
e = mid - 1;
}
// Equal!
else {
return mid;
}
}
return -1;
}
private static int binarySearch(int[] array, int K) {
int n = array.length;
int s = 0;
int e = n - 1;
while (s <= e) {
// Compute Mid
int mid = (s + e)/2;
// Go Right
if (array[mid] < K) {
s = mid + 1;
}
// Go Left
else if (array[mid] > K) {
e = mid - 1;
}
// Equal!
else {
return mid;
}
}
return -1;
}