-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySearch.html
More file actions
39 lines (35 loc) · 796 Bytes
/
binarySearch.html
File metadata and controls
39 lines (35 loc) · 796 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
33
34
35
36
37
38
39
<!DOCTYPE html>
<html>
<head>
<title>Count</title>
</head>
<body>
<script>
function array_binarySearch(narray, delement) {
var mposition = Math.floor(narray.length / 2);
if (narray[mposition] === delement){
return mposition;
}
else if (narray.length === 1){
return null;
}
else if (narray[mposition] < delement) {
var arr = narray.slice(mposition + 1);
var res = array_binarySearch(arr, delement);
if (res === null){
return null;
}
else {
return mposition + 1 + res;
}
}
else {
var arr1 = narray.slice(0, mposition);
return array_binarySearch(arr1, delement);
}
}
var myArray = [1, 2, 3, 5, 6, 7, 10, 11, 14, 15, 17, 19, 20, 22, 23];
console.log(array_binarySearch(myArray, 6));
</script>
</body>
</html>