Last active
August 29, 2015 14:23
-
-
Save Cartman0/754bef17d04d4f6bc012 to your computer and use it in GitHub Desktop.
JavaScript でBinarySearch
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
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