Update binarySearch.md

This commit is contained in:
Isabelle Viktoria Maciohsek
2020-12-29 13:06:47 +02:00
committed by GitHub
parent 3e1e383386
commit 663fb78277

View File

@ -14,16 +14,13 @@ Finds the index of a given element in a sorted array using the binary search alg
const binarySearch = (arr, item) => { const binarySearch = (arr, item) => {
let l = 0, let l = 0,
r = arr.length - 1; r = arr.length - 1;
while (l <= r) { while (l <= r) {
const mid = Math.floor((l + r) / 2); const mid = Math.floor((l + r) / 2);
const guess = arr[mid]; const guess = arr[mid];
if (guess === item) return mid; if (guess === item) return mid;
if (guess > item) r = mid - 1; if (guess > item) r = mid - 1;
else l = mid + 1; else l = mid + 1;
} }
return -1; return -1;
}; };
``` ```