Created
March 31, 2022 11:48
-
-
Save prav-raghu/c61fb7f3f5e00aaf84653a687a44bf2f to your computer and use it in GitHub Desktop.
Recursive Binary Search in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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