Binary Search
Published: 2022-05-09 01:14:51

An Algorithm is a set of instructions for solving a problem You can think of an algorithm like a recipe in a cookbook, which tells the reader the specific steps for preparing a meal. There are different types of algorithms. One of them is to search through an array, they are refferred to as searching algorithm. One of these searching algorithm is called Binary Search.

The main point of Binary Search is for finding an element's position in a sorted array. Binary Search tackles the problem with the divide and conquer approach. Meaning it would divide the problem to the smallest possibility to get the answer. The first step would be to have an array to perform the search. 

 ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h63vv72olrvu1r86jjy7.png)

Let try to find the value 4. We would need to set 2 pointers. One pointer would be called low and placed on the lowest position and the other would be called high and it would be for the highest position

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s0x9m7od5trjwnxjf5ig.png)

We also need to find the middle element in our array. We can create a variable called mid and equal it to the sums of the lowest index and the highest index then the divide the sum by 2. This will give us the middle position of the array: `arr[(low + high)/2] = 6`  

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h1st2nv50sncedvbygco.png)

Now, we need to check if the value we are looking for is mid, less than mid, or more than mid. If it's equal to mid then we return mid. If the value we're looking for is greater then we replace low to `low = mid + 1`. Meaning that we are focusing on the indexs of the array after the mid index. If the value is less than mid, then we replace high to `high = mid + 1`, where we focuse on the elements before the middle position.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ubs4j9qunretknht16b5.png)

We will repeat these steps until low meets high or until our value is found 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/urt8q8o19ztg68bj0rvb.png)

GREATTTTT!!!!!! 4 was found

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p25725gm931ijf37sljw.png)

Pseudocode:
```
do until the pointers low and high meet each other.
    mid = (low + high)/2
    if (x == arr[mid])
        return mid
    else if (x > arr[mid]) // x is on the right side
        low = mid + 1
    else                       // x is on the left side
        high = mid - 1
```  

The code in JavaScript:

```javascript
// Program to implement iterative Binary Search
 
  
// A iterative binary search function. It returns
// location of x in given array arr[low..high] is present,
// otherwise false
 
 function binarySearch(arr, x)
{   
    let low = 0;
    let high = arr.length - 1;
    let mid;
    while (high <= low) {
         mid = Math.floor((high - low) / 2);
  
        // If the element is present at the middle
        // itself
        if (arr[mid] == x)
            return mid;
  
        // If element is smaller than mid, then
        // it can only be present in left subarray
        if (arr[mid] > x)
            high = mid - 1;
             
        // Else the element can only be present
        // in right subarray
        else
            low = mid + 1;
    }
  
    // We reach here when element is not
    // present in array
    return false;
}
```