Easiest way to learn Binary Search

Easiest way to learn Binary Search

Defination :

Binary Search is an algorithm used to search for an specific element in an array of integers , Array must be sorted either in decreasing order or non-decreasing order (increasing order).

Thats it thats all about the simple defination ,Let's see how binary search acutally works

so you have given a sorted array containing integers .

ex : [1,4,6,8,9,11]

now we have to take two pointers (varibles):

  • start = this variable will have the index of first element of an array.

  • end = this variable will have the index of last element of an array.

now the value of the both variables will be as follows:

start = 0 ;

end = 6 (arr.length-1 // this method returns the length of an array and taking -1 because index starts from 0 )

one more variable we will require and that is index of middle element

in the example of [1,4,6,8,9,11] our middle will be 3, and the element lies on index 3 is [8]

But how we came up with 3 ?

so the formula for calculating the middle element is simple

start+end /2 ;

0+6/2 = 3

thats how we came up with 3 .

I know you are eagerly waiting for the approch so lets begin the main approch of binary search

ex: [1,4,6,8,9,11]

suppose we have to find the element 9 in the array if it is available in the array we will return the index otherwise we will return -1;

so we all know element 9 lies on 5th index but but our compiler isnt smart enough like us , he doesnt know where the element 9 lies so we have to instruct him

  • step 1 : we have our middle element we just have to compare our target element which is 9 with the element lies on middle index that is 8 ,

ex: [1,4,6,8,9,11]

bold element is our middle element

compare using if statement

ex: if(arr[middle]==target)

if(8==9) we all know middle is not equal to the target

  • step 2 : now check the middle element is greater or smaller than the target

ex : if(arr[middle]>arr[target])

      if(8>9)

so now we know that our middle element (8) is smaller than target , as the array is in increasing order is their any point looking the left side of an array [{1,4,6,}8,9,11] (elements in the curly braces are left hand side of an array ) . we are not dumb like complier we know where to search

  • step 3 : as the element is greater than middle element we will only search on the right hand side [1,4,6,8,{9,11}]

  • step 4 : now we will update our start element as middle +1 that is 4 and the element lies on index 4 is 9

  • step 5 : again we will calculate our middle element as per formula

start +end /2

4+5/2 =4

our middle index is 4 and element lies on index 4 is 9 [1,4,6,8,{9},11]

  • step 6 : that's it our middle element is equal to the target element we will return middle from our function

hope you understood the main concept if the middle element is greater than target element then simple! we have to search on left hand side

That's it from the blog ,

I will make more blogs on some other algorithms

java code is given below bye !

package Searching;

public class BinarySearch {
    public static int BinarySearch1(int[] arr ,int target ){
        int start = 0 ;
        int end = arr.length-1;
        // taking a while loop till end is greater than start
        while(start<=end){
            int middle = (start+end)/2; // calculating the middle as per formula
            // checking all the three conditions
            if(arr[middle]==target){
                // it is simple if the middle element is equal to target then simple return the middle
                return middle;
            }
            else if(arr[middle]>target){
                // end will become mid -1 because we only have to check left hand side of an array
                end = middle-1;
            }
            else {
                // start will become mid +1 because we only have to check right hand side of an array

                start =middle+1;
            }
        }
        return -1;
    }
    public static void main(String[] args) {
        int [] arr ={1,4,6,8,9,11};
        System.out.println(BinarySearch1(arr,9));

    }
}