Skip to content

Instantly share code, notes, and snippets.

@goldeneggg
Created June 11, 2014 09:21
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 goldeneggg/fba26b6d63f990439670 to your computer and use it in GitHub Desktop.
Save goldeneggg/fba26b6d63f990439670 to your computer and use it in GitHub Desktop.
Goで某言語のように存在しなかったら-1を返す版のバイナリサーチ
func BinarySearch(data []int, find int, from int, to int) int {
center := (from + to) / 2
if from > to {
return -1
}
if find == data[center] {
return center
} else if find < data[center] {
return BinarySearch(data, find, from, center-1)
} else {
return BinarySearch(data, find, center+1, to)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment