How Binary Search Works with Example

How Binary Search Works with Example


Binary Search : 

To find an value in array we are having different ways and binary search is one of the most efficient way. It will reduce the no. of iteration to find the solution thus get us the solution quickly.

In simple form, we need to do 3 checks as follows : 

target value = the value to find in the array.

    1. if the target value = array[mid] => return the index.

    2. if target value is smaller, than start searching on the left side else search on right side.

    3. Keep repeating the process till either you get the value or till you iterate elements in array.


How Binary Search Works : 

Learn by solving a Problem : 

Write a method to find an index of an element in an ascending Sorted Array using Binary Search. 

If element doesn't exist return -1.

Illustration 1 :    a[] = {1,2,4,5,7,8,10} , Find 5 

                            Output =>  3  

                            Explanation : Index of 5 is 3.

Illustration 2 :    a[] = {1,2,4,5,7,8,10} , Find 6 

                            Output =>  -1.

                            Explanation :  As 6 is not available in array.

Solution :
                                 public int BinarySearch(int[] arr, int target) 
    {
        //int a = -1;
        
        // First Approach - Using IndexOf
        //a = Array.IndexOf(arr,target);
        
        //Second Approach -- Linear Search         
        //for(int index =0 ; index < arr.Length; index++)
        //{
        //    if(arr[index] == target)
        //    {
        //        a = index;
        //        break;
        //    }
        //}
        
        // Third Approach - Binary Search
        int arrayLength = arr.Length;
                
        if(arrayLength == 1)
        {
            if(arr[0] == target)
                return 0;
            else
                return -1;
        }
        else
        {
            int center, first = 0, last = arrayLength - 1;
            while (first < last || first == last)
            {
                center = first + ((last - first) / 2);  // Check the Link 1
                if(arr[center] == target)
                    return center;        
                if(arr[center] < target)
                    first = center + 1;
                else
                    last = center - 1;                
            }            
        }        
        return -1;                
    }

Input :    arr [] = {-1,0,3,5,9,12,15,19,21,25,27,29,30}; target = 17

Output : -1

Step 1 : Initializing values

          center, firstIndex = 0, lastIndex = 12

Step 2 : Find center value and check arr[center] with Target value

 firstIndex : 0 lastIndex : 12

 center : 6 target :17

 center : 6 , arr[center] : 15 target :17

Step 3: As  arr[center] > target  => firstIndex = center + 1

 firstIndex : 7 lastIndex : 12

 center : 9 target :17

 center : 9 , arr[center] : 25 target :17

Step 3: As  arr[center] <  target  => lastIndex = center  - 1

 firstIndex : 7 lastIndex : 8

 center : 7 target :17

 center : 7 , arr[center] : 19 target :17

Step 4 : It came out of loop. 

Link 1 : Explanation why we put (firstIndex + (lastIndex - firstIndex)/2) as value


#HappyLearning

Comments

Popular posts from this blog

Border Radius not working in Outlook Email template

C# .Net Core Interview Questions for Freshers and Experienced Developers

15 ways to keep your team motivated while working remotely ?