Skip to content

Commit 8bbe43d

Browse files
committed
No frills implementation of Binary Search.
1 parent 721bad0 commit 8bbe43d

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var binarySearch = function(num,inputArray){
2+
var minIndex = 0;
3+
var maxIndex = inputArray.length-1;
4+
var currIndex;
5+
var currElement;
6+
while(minIndex<=maxIndex){
7+
currIndex = (maxIndex-minIndex)/2|0;
8+
currElement = inputArray[currIndex];
9+
if(num<currElement){
10+
maxIndex = currIndex-1;
11+
}else if(num>currElement){
12+
minIndex = currIndex+1;
13+
}
14+
else{
15+
return currIndex;
16+
}
17+
}
18+
return -1;
19+
}

0 commit comments

Comments
 (0)