Skip to content

Instantly share code, notes, and snippets.

@prav-raghu
Created March 31, 2022 11:48
Show Gist options
  • Save prav-raghu/c61fb7f3f5e00aaf84653a687a44bf2f to your computer and use it in GitHub Desktop.
Save prav-raghu/c61fb7f3f5e00aaf84653a687a44bf2f to your computer and use it in GitHub Desktop.
Recursive Binary Search in JavaScript
var array = [1,2,3,4,5]
let recursiveFunction = function(array,x,start,end){
if(start > end){
return false
}
let mid = Math.floor((start + end)/2)
if(array[mid] === x){
return true;
}
if(array[mid] > x){
return recursiveFunction(array,x,start,mid-1);
}
else{
return recursiveFunction(array,x,start+1,end);
}
}
console.log(recursiveFunction(array,6,0,array.length))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment