Skip to content

Commit 3976243

Browse files
committed
BinarySearch
1 parent 341f8bd commit 3976243

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

SearchAlgorithms/BinarySearch.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//Language: Javascript
2+
//Author: Tran Lan Anh
3+
//Github: https://github.com/Lanhbaobao
4+
5+
var binarySearch = function(array, value) {
6+
var guess,
7+
min = 0,
8+
max = array.length - 1;
9+
10+
while(min <= max){
11+
guess = Math.floor((min + max) /2);
12+
if(array[guess] === value)
13+
return guess;
14+
else if(array[guess] < value)
15+
min = guess + 1;
16+
else
17+
max = guess - 1;
18+
}
19+
20+
return -1;
21+
}
22+
23+
var arr = [2,6,4,1,7,9];
24+
25+
binarySearch(arr, 4);

0 commit comments

Comments
 (0)