Skip to content

Instantly share code, notes, and snippets.

@jajoosam
Created August 12, 2016 14:17
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 jajoosam/2ea6523a1cdedf85b3439742291ff78f to your computer and use it in GitHub Desktop.
Save jajoosam/2ea6523a1cdedf85b3439742291ff78f to your computer and use it in GitHub Desktop.
Codjo #2 - by @leotrs and @jajoosam
function sortedSearch(listy, x) {
var start = 0;
var end = listy.length-1;
var index = Math.round((end+start)/2);
var i = 0;
while (listy[index] != x && i<listy.length/2) {
if(x<listy[index]){
end = index;
}
else {
start = index;
}
index = Math.round((end+start)/2);
i++;
}
if(i<listy.length/2){
return index;
}
else{
return -1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment