Today, our study group went over two approaches for implementing binary search – recursive & iterative – and O(log n) time complexity.

We talked about “divide and conquer” algorithms, and why this particular way of finding the target number in an array by finding the midpoint of each sub-array, cut the number of objects to compare with in half with each operation. This is what gives the method logarithmic O(log n) time complexity.

Here is the iterative way of solving Binary Search in Javascript:

`let iterativeFunction = `**function**

`(arr, x) {`

` let start=0, end=arr.length-1;`

` `**while**

`(start<=end){`

` let mid=Math.floor((start + end)/2);`

` `**if**

`(arr[mid]===x) `**return**

**true**;

` `**else**

**if**

`(arr[mid] < x)`

` start = mid + 1;`

` `**else**

` end = mid - 1;`

` }`

` `**return**

**false**;

`}`

Here is the recursive way of solving Binary Search in Javascript:

`let recursiveFunction = `**function**

`(arr, x, start, end) {`

` // Base Condition`

` `**if**

`(start > end) `**return**

**false**;

` // Find the middle index`

` let mid=Math.floor((start + end)/2);`

` // Compare mid with given key x`

` `**if**

`(arr[mid]===x) `**return**

**true**;

` `

` // If element at mid is greater than x,`

` // search in the left half of mid`

` `**if**(arr[mid] > x)

` `**return**

`recursiveFunction(arr, x, start, mid-1);`

` `**else**

` // If element at mid is smaller than x,`

` // search in the right half of mid`

` `**return**

`recursiveFunction(arr, x, mid+1, end);`

`}`

[A deeper explanation is available in the GeekforGeek link below]

Our study group also helped each other troubleshoot some git & dev environment setup issues.