Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions DSA Javascript/Binary_search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<script>
let recursiveFunction = function (arr, x, start, end) {

// Base Condition
if (start > end) return false;

// Find the middle index
let mid=Math.floor((start + end)/2);

// Compare mid with given key x
if (arr[mid]===x) return true;

// If element at mid is greater than x,
// search in the left half of mid
if(arr[mid] > x)
return recursiveFunction(arr, x, start, mid-1);
else

// If element at mid is smaller than x,
// search in the right half of mid
return recursiveFunction(arr, x, mid+1, end);
}

// Driver code
let arr = [1, 3, 5, 7, 8, 9];
let x = 5;

if (recursiveFunction(arr, x, 0, arr.length-1))
document.write("Element found!<br>");
else document.write("Element not found!<br>");

x = 6;

if (recursiveFunction(arr, x, 0, arr.length-1))
document.write("Element found!<br>");
else document.write("Element not found!<br>");
</script>