Search Flashcards

1
Q

Find the value 3 using binary search in the following array: [0,1,2,3,4,5,6,7]?

A
function binarySearch(arr, target){
 var min = 0; 
 var max = arr.length - 1;
 while(min \<= max){
 var mid = Math.floor((min + max) / 2);
 if(target \< arr[mid]){
 max = mid - 1;
 }
 else if (target \> arr[mid]){
 min = mid + 1;
 }
 else {
 return mid;
 }
 }
 return false;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly