-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinarysearch.js
40 lines (37 loc) · 1.18 KB
/
binarysearch.js
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
33
34
35
36
37
38
39
40
//Function added to native's Array prototype, returns the index of the element we're looking for
function binarySearch(searchElement) {
var minIndex = 0, maxIndex = this.length - 1, currentIndex;
while (minIndex <= maxIndex) {
//num | 0 is the same as Math.floor(num) but a bit faster
currentIndex = (minIndex + maxIndex) / 2 | 0;
if (this[currentIndex] < searchElement) {
minIndex = currentIndex + 1;
}
else if (this[currentIndex] > searchElement) {
maxIndex = currentIndex - 1;
}
else {
return currentIndex;
}
}
return -1;
}
var sorted = [1, 3, 5, 6, 7, 23, 33, 34, 55, 69];
binarySearch.call(sorted, 69);
// Recursive implementation of the Binary Search algorithm
function binarySearch(sortedArray, elem, start, end) {
var half = (start + end) / 2 | 0;
if (start > end) {
return -1;
}
// search on the right half
if (sortedArray[half] < elem) {
return binarySearch(sortedArray, elem, half + 1, end);
}
// search on the left half
if (sortedArray[half] > elem) {
return binarySearch(sortedArray, elem, 0, half - 1);
}
return half;
}
binarySearch(sorted, 69, 0, sorted.length-1);