From 04823be518b87b120333b090789009f2ffdd6349 Mon Sep 17 00:00:00 2001 From: Eric Weiss <40743521+kinnybot@users.noreply.github.com> Date: Sun, 14 Mar 2021 11:43:52 -0400 Subject: [PATCH] return position (#188) --- 01_introduction_to_algorithms/Golang/BinarySearch.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/01_introduction_to_algorithms/Golang/BinarySearch.go b/01_introduction_to_algorithms/Golang/BinarySearch.go index 04724d7..988f541 100644 --- a/01_introduction_to_algorithms/Golang/BinarySearch.go +++ b/01_introduction_to_algorithms/Golang/BinarySearch.go @@ -2,13 +2,13 @@ package main import "fmt" -func checkBin(list []int, i int) bool { +func checkBin(list []int, i int) int { low := 0 high := len(list) - 1 for low <= high { mid := (low + high) / 2 if list[mid] == i { - return true + return mid } if list[mid] < i { low = mid + 1 @@ -16,10 +16,10 @@ func checkBin(list []int, i int) bool { high = mid - 1 } } - return false + return -1 } func main() { - fmt.Println(checkBin([]int{1, 2, 3, 4, 5}, 1)) // true - fmt.Println(checkBin([]int{1, 2, 3, 4, 5}, -1)) //false + fmt.Println(checkBin([]int{1, 2, 3, 4, 5}, 1)) // 0 + fmt.Println(checkBin([]int{1, 2, 3, 4, 5}, -1)) // -1 }