Skip to content

Instantly share code, notes, and snippets.

@Cartman0
Last active August 29, 2015 14:23
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 Cartman0/754bef17d04d4f6bc012 to your computer and use it in GitHub Desktop.
Save Cartman0/754bef17d04d4f6bc012 to your computer and use it in GitHub Desktop.
JavaScript でBinarySearch
function binary_search(a, start_idx, end_idx, target){
var start = start_idx;
var end = end_idx;
while((end - start) >= 0){
var mid = Math.floor((end + start) / 2);
if(a[mid] === target){
return {
flag: true,
index: mid
};
}else if(a[mid] > target){
end = mid - 1;
}else {
start = mid + 1;
}
}
return {
flag: false,
index: -1
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment