Binary Search

Prateek Narang

Binary Search

What you will learn?

  • 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

Searching

 0       1       2       3       4       5        6       7       8

Lets try to search for Number 12 in an array.

 1     3    4    5    9   12  22  38  45

 0       1       2       3       4       5        6       7       8

Lets try to search for Number 12 in an array.

 1     3    4    5    9   12  22  38  45

12

 0       1       2       3       4       5        6       7       8

Lets try to search for Number 12 in an array.

 1     3    4    5    9   12  22  38  45

12

 0       1       2       3       4       5        6       7       8

Lets try to search for Number 12 in an array.

 1     3    4    5    9   12  22  38  45

12

 0       1       2       3       4       5        6       7       8

Lets try to search for Number 12 in an array.

 1     3    4    5    9   12  22  38  45

12

 0       1       2       3       4       5        6       7       8

Lets try to search for Number 12 in an array.

 1     3    4    5    9   12  22  38  45

12

 0       1       2       3       4       5        6       7       8

Lets try to search for Number 12 in an array.

 1     3    4    5    9   12  22  38  45

12

 0       1       2       3       4       5        6       7       8

Lets try to search for Number 12 in an array.

 1     3    4    5    9   12  22  38  45

12

and there is a Match at index 5!

This algorithm of linearly searching through the array is known as

Linear Search!

  • Can we do better?

  • Can we exploit the information that the array is sorted?

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.

🚀 Binary Search

10, 20, 28 , 56, 100

(Sorted Array)

 0       1       2       3       4       5        6       7       8

Lets try to search for Number 12 in an array.

s

e

Initialise

s = 0, e = 8

 1     3    4    5    9   12  22  38  45

 0       1       2       3       4       5        6       7       8

Lets try to search for Number 12 in an array.

s

e

Find the midpoint
s = 0, e = 8

mid = (s+e)/2

        = (0+8)/2

        = 4

mid

 1     3    4    5    9   12  22  38  45

 0       1       2       3       4       5        6       7       8

Lets try to search for Number 12 in an array.

s

e

mid

Make a comparison between

arr[mid] & key

 

 

 1     3    4    5    9   12  22  38  45

12

9 <

 0       1       2       3       4       5        6       7       8

s

e

mid

9 <

Clearly

 

So, the we discard the part of the array from s to mid.

12

 1     3    4    5    9   12  22  38  45

 0       1       2       3       4       5        6       7       8

s

e

 1     3    4    5    9   12  22  38  45

s = mid + 1

Hence, the new start becomes 5.

mid

 0       1       2       3       4       5        6       7       8

s

e

mid

mid = (s + e)/2

       =  (5 + 8)/2

       =  6

 

 1     3    4    5    9   12  22  38  45

 0       1       2       3       4       5        6       7       8

s

e

mid

Now again, we compare a[mid] and key

22 >

12

 1     3    4    5    9   12  22  38  45

 0       1       2       3       4       5        6       7       8

s

e

mid

a[mid] >

key

 1     3    4    5    9   12  22  38  45

e = mid - 1

 0       1       2       3       4       5        6       7       8

s

e

mid = (5 + 5)/2

        = 5

mid

 1     3    4    5    9   12  22  38  45

 0       1       2       3       4       5        6       7       8

s

e

mid

a[mid] == 

12

 1     3    4    5    9   12  22  38  45

 0       1       2       3       4       5        6       7       8

s

e

mid

a[mid] == 

return mid;

12

 1     3    4    5    9   12  22  38  45

 0       1       2       3       4       5        6       7       8

s

e

mid

and we got the answer that the final index of 12 is actually 5.

 1     3    4    5    9   12  22  38  45

What If, the element is 

NOT PRESENT 🧐

 0       1       2       3       4       5        6       7       8

Lets try to search for Number 6 in an array.

s

e

 1     3    4    5    9   12  22  38  45

 0       1       2       3       4       5        6       7       8

Lets try to search for Number 6 in an array.

s

e

mid

 1     3    4    5    9   12  22  38  45

mid = (0 + 8)/2 = 4

 0       1       2       3       4       5        6       7       8

Lets try to search for Number 6 in an array.

s

e

mid

9 >

Search in the left of mid

6

 1     3    4    5    9   12  22  38  45

 0       1       2       3       4       5        6       7       8

Lets try to search for Number 6 in an array.

s

e

mid

e = mid - 1

 1     3    4    5    9   12  22  38  45

 0       1       2       3       4       5        6       7       8

Lets try to search for Number 6 in an array.

e

mid = (0 + 3)/2 = 1

mid

 1     3    4    5    9   12  22  38  45

s

 0       1       2       3       4       5        6       7       8

Lets try to search for Number 6 in an array.

s

e

mid

Search in the right of mid

6

3  <

 1     3    4    5    9   12  22  38  45

 0       1       2       3       4       5        6       7       8

Lets try to search for Number 6 in an array.

s

e

mid

Search in the right of mid

6

3  <

 1     3    4    5    9   12  22  38  45

 0       1       2       3       4       5        6       7       8

Lets try to search for Number 6 in an array.

e

mid

s = mid + 1

6

3  <

s

 1     3    4    5    9   12  22  38  45

 0       1       2       3       4       5        6       7       8

Lets try to search for Number 6 in an array.

e

s = mid + 1

6

3  <

s

 1     3    4    5    9   12  22  38  45

mid

 0       1       2       3       4       5        6       7       8

Lets try to search for Number 6 in an array.

e

mid = (2 + 3)/2 = 2

s

mid

 1     3    4    5    9   12  22  38  45

 0       1       2       3       4       5        6       7       8

Lets try to search for Number 6 in an array.

e

Compare

s

mid

4 <

6

 1     3    4    5    9   12  22  38  45

 0       1       2       3       4       5        6       7       8

Lets try to search for Number 6 in an array.

e

Go Right, s = mid + 1

s

mid

4 <

6

 1     3    4    5    9   12  22  38  45

 0       1       2       3       4       5        6       7       8

Lets try to search for Number 6 in an array.

e

Go Right, s = mid + 1

s

mid

4 <

6

 1     3    4    5    9   12  22  38  45

 0       1       2       3       4       5        6       7       8

Lets try to search for Number 6 in an array.

e

s

mid

mid = (3 + 3)/2 = 3

 1     3    4    5    9   12  22  38  45

 0       1       2       3       4       5        6       7       8

Lets try to search for Number 6 in an array.

e

s

mid

Go right, s = mid + 1

5 <

6

 1     3    4    5    9   12  22  38  45

 0       1       2       3       4       5        6       7       8

Lets try to search for Number 6 in an array.

e

s

mid

Go right, s = mid + 1

5 <

6

 1     3    4    5    9   12  22  38  45

 0       1       2       3       4       5        6       7       8

Lets try to search for Number 6 in an array.

e

s

start becomes greater than end! 🧐

s > e

 1     3    4    5    9   12  22  38  45

mid

 0       1       2       3       4       5        6       7       8

Lets try to search for Number 6 in an array.

e

s

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.

 1     3    4    5    9   12  22  38  45

Binary Search is a Divide & Conquer Algorithm.

Binary Search

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.

Code 👨‍💻 

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

Complexity Analysis

Time? 👨‍💻 

Let's dicuss the worst complexity

 1     3    4    5    9   12  22  38  45

 1     3    4    5    9   12  22  38  45

 1     3    4    5    9   12  22  38  45

Iteration ,  Array Size

1       N/2 

 

 

2      N/4

 

 

3    N/8

.

.

Stop

 

 

 1     3    4    5    9   12  22  38  45

 1     3    4    5    9   12  22  38  45

 1     3    4    5    9   12  22  38  45

 1     3    4    5    9   12  22  38  45

 1     3    4    5    9   12  22  38  45

 1     3    4    5    9   12  22  38  45

 1     3    4    5    9   12  22  38  45

 1     3    4    5    9   12  22  38  45

 1     3    4    5    9   12  22  38  45

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

 1     3    4    5    9   12  22  38  45

 1     3    4    5    9   12  22  38  45

 1     3    4    5    9   12  22  38  45

 1     3    4    5    9   12  22  38  45

 1     3    4    5    9   12  22  38  45

 1     3    4    5    9   12  22  38  45

 1     3    4    5    9   12  22  38  45

 1     3    4    5    9   12  22  38  45

 1     3    4    5    9   12  22  38  45

 1     3    4    5    9   12  22  38  45

 1     3    4    5    9   12  22  38  45

 1     3    4    5    9   12  22  38  45

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;
}

Space? 👨‍💻 

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;
}

Thank you! 👨‍💻 

[Scaler] Binary Search

By Prateek Narang

[Scaler] Binary Search

  • 112