Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function binarySearch(arr, target) {
- let min = 0;
- let max = arr.length - 1;
- while (min <= max) {
- const mid = min + Math.floor((max - min) / 2);
- if (arr[mid] === target) {
- return mid;
- }
- if (arr[mid] < target) {
- min = mid++;
- } else {
- max = mid--;
- }
- }
- return -1;
- }
Add Comment
Please, Sign In to add comment