Last active
July 27, 2016 16:36
-
-
Save eternal44/338d8b9248dcad210e018369eee1d2b3 to your computer and use it in GitHub Desktop.
Finds the index of an integer in a sorted array
This file contains hidden or 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
function bsearch(ns, n) { | |
var startPoint = 0 | |
var endPoint = ns.length - 1 | |
var midPoint | |
while((startPoint < (endPoint + 1))) { | |
midPoint = Math.floor((endPoint - startPoint) / 2) + startPoint | |
if(ns[midPoint] === n){ | |
return midPoint | |
} | |
if(ns[midPoint] > n) { | |
endPoint = midPoint - 1 | |
} else if(ns[midPoint] < n) { | |
startPoint = midPoint + 1 | |
} | |
} | |
return -1 | |
} | |
console.log(bsearch([1,2,3,4,5,6,7,8], 4)); | |
console.log(bsearch([1,2,3,4,5,6,7,8,9], 4)); | |
console.log(bsearch([1,2,3,4,5,6,7,8,9], 2)); | |
console.log(bsearch([1,2,3,4,5,6,7,8,9], 1)); | |
console.log(bsearch([1,2,3,4,5,6,7,8,9], 9)); | |
console.log(bsearch([1,2,3,4,5,6,7,8,9], 7)); | |
console.log(bsearch([1,2,3,4,5,6,7,8], 10)); | |
console.log(bsearch([], 10)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment