-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathbinary-search.js
More file actions
32 lines (25 loc) · 850 Bytes
/
binary-search.js
File metadata and controls
32 lines (25 loc) · 850 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/* Binary Search Implementation in JavaScript */
function binarySearch(arr, item) {
var low = 0,
high = arr.length - 1,
mid, found = false;
while (low <= high) {
mid = Math.floor((low + high) / 2); //Midpoint of the Search Space
if (arr[mid] === item) { //Found the element
found = true;
break;
} else {
if (item < arr[mid]) { //Need to search in the left half of the search space
high = mid - 1;
} else { //Need to search in the right half of the search space
low = mid + 1;
}
}
}
return found;
}
/************ Testing Binary Search Implementation ***************/
// This list must be sorted. If it is not given as sorted, sort it first, then call the binarySearch method
var testList = [0, 1, 2, 8, 13, 17, 19, 32, 42];
console.log(binarySearch(testList, 3));
console.log(binarySearch(testList, 13));