Created
June 11, 2014 09:21
-
-
Save goldeneggg/fba26b6d63f990439670 to your computer and use it in GitHub Desktop.
Goで某言語のように存在しなかったら-1を返す版のバイナリサーチ
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
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