Skip to content

Instantly share code, notes, and snippets.

@hillal20
Created October 1, 2018 03:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hillal20/7a4d21b97565f5c518a95027f3fa1da1 to your computer and use it in GitHub Desktop.
Save hillal20/7a4d21b97565f5c518a95027f3fa1da1 to your computer and use it in GitHub Desktop.
PeriodicLazyCoordinates created by hillal20 - https://repl.it/@hillal20/PeriodicLazyCoordinates
let a = [1,8,4,5,9]
function bs(l,h,arr,key){
let sArr = arr.sort((a,b)=>{
return a > b
})
let middle = Math.floor((l+h+1/2))
if( l === h ){
if (sArr[l] === key){
return true
}
}
else {
if (sArr[middle]=== key){
return true
}
else if (key < sArr[middle]){
return bs(l, middle-1,sArr,key)
}
else if (key > sArr[middle]){
return bs(middle + 1, h ,sArr,key)
}
return false
}
}
console.log(bs(0,a.length-1,a,8))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment