forked from akshitagit/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary_Search.js
More file actions
35 lines (21 loc) · 749 Bytes
/
Binary_Search.js
File metadata and controls
35 lines (21 loc) · 749 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
//JavaScript Implementation of Binary Search
<script>
let RecursiveFunction = function (arr, x, start, end) {
if (start > end) return false;
let mid=Math.floor((start + end)/2);
if (arr[mid]===x) return true;
if(arr[mid] > x)
return RecursiveFunction(arr, x, start, mid-1);
else
return RecursiveFunction(arr, x, mid+1, end);
}
let arr = [11, 13, 15, 17, 18, 19];
let x = 17;
if (RecursiveFunction(arr, x, 0, arr.length-1))
document.write("Element found!<br>");
else document.write("Element not found!<br>");
x = 6;
if (ReursiveFunction(arr, x, 0, arr.length-1))
document.write("Element found!<br>");
else document.write("Element not found!<br>");
</script>