Files
box-o-sand/algs4/src/go/algs4/binarysearch.go
Dan Buch 6ebbf04980 Re-namespacing one more time to get sources back in the same tree.
I dunno...  this whole thing should be its own repo, probably.  Meh.
2012-12-19 22:33:46 -05:00

22 lines
267 B
Go

package algs4
func BinarySearchRank(key int, a []int) int {
lo := 0
hi := len(a) - 1
var mid int
for lo <= hi {
mid = lo + (hi-lo)/2
if key < a[mid] {
hi = mid - 1
} else if key > a[mid] {
lo = mid + 1
} else {
return mid
}
}
return -1
}