We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 341f8bd commit 3976243Copy full SHA for 3976243
1 file changed
SearchAlgorithms/BinarySearch.js
@@ -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