const binarySearch = (arr, ele) => { let start = 0 let end = arr.length - 1 let mid = Math.floor((start + end)/2) while(ele !== arr[mid] && start <= end) { if(ele < arr[mid]) end = mid - 1 else start = mid + 1 mid = Math.floor((start + end)/2) } return arr[mid] === ele ? mid : -1 } binarySearch([1,2,6,3,9,10,43,90], 90)
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter