英文:
Go recursive binary search
问题
我知道Go语言有一个sort包,其中包含搜索函数,但这只是为了教育目的。我一直在尝试在Go中实现二分搜索算法,但是我无法使其正常工作。
这是我的代码:
package main
import "fmt"
func BinarySearch(data []int, target int, low int, high int) (index int, found bool) {
mid := (high + low) / 2
if low > high {
index = -1
found = false
} else {
if target < data[mid] {
BinarySearch(data, target, low, mid - 1)
} else if target > data[mid] {
BinarySearch(data, target, mid + 1, high)
} else if target == data[mid] {
index = mid
found = true
} else {
index = -1
found = false
}
}
return
}
func main() {
data := []int {2, 4, 6, 8, 9, 11, 12, 24, 36, 37, 39, 41, 54, 55, 56,}
index, found := BinarySearch(data, 8, 0, len(data) - 1)
fmt.Println(index, found)
}
它总是打印出0 false。为什么呢?
英文:
I know that Go has a sort package that contains search functions, but this is for educational purposes. I've been trying to implement a binary search algorithm in Go but I haven't been able to get it to work.
Here is my code:
package main
import "fmt"
func BinarySearch(data []int, target int, low int, high int) (index int, found bool) {
mid := (high + low) / 2
if low > high {
index = -1
found = false
} else {
if target < data[mid] {
BinarySearch(data, target, low, mid - 1)
} else if target > data[mid] {
BinarySearch(data, target, mid + 1, high)
} else if target == data[mid] {
index = mid
found = true
} else {
index = -1
found = false
}
}
return
}
func main() {
data := []int {2, 4, 6, 8, 9, 11, 12, 24, 36, 37, 39, 41, 54, 55, 56,}
index, found := BinarySearch(data, 8, 0, len(data) - 1)
fmt.Println(index, found)
}
It always prints 0 false. Why?
答案1
得分: 5
你的二分查找逻辑是正确的。唯一的问题是你忘记了将每个递归调用的结果赋值给index和found。
目前你有这些递归调用:
BinarySearch(data, target, low, mid - 1)
//...
BinarySearch(data, target, mid + 1, high)
你只需要将结果赋值即可:
index, found = BinarySearch(data, target, low, mid - 1)
//...
index, found = BinarySearch(data, target, mid + 1, high)
英文:
The logic of your binary search is sound. The only problem is that you've forgotten to assign the result of each recursive call to index and found.
Currently you have these recursive calls:
BinarySearch(data, target, low, mid - 1)
//...
BinarySearch(data, target, mid + 1, high)
You merely have to assign the results:
index, found = BinarySearch(data, target, low, mid - 1)
//...
index, found = BinarySearch(data, target, mid + 1, high)
答案2
得分: 1
二分查找的逻辑
- 目标是在数组中搜索一个元素。
- 获取数组的中间元素。
- 如果大于目标值 => 从第一个元素到最后一个元素。
- 如果小于目标值 => 从中间元素到最后一个元素。
- 重复这个过程。
我们可以使用递归或循环来实现这个算法。
使用递归实现二分查找
函数将包括一个数组,在这个数组中进行搜索。目标值等于我们要搜索的值。
lowIndex 表示搜索的起始位置。
highIndex 表示搜索的最后位置。
然后函数返回我们要搜索的目标值的位置。
将 lowIndex 和 highIndex 包含在参数中的原因是为了搜索数组的子集。
func binarySearch(array []int, target int, lowIndex int, highIndex int) int {
// 指定递归结束的条件
if highIndex < lowIndex {
return -1
}
// 定义中间索引
mid := int((lowIndex + highIndex) / 2)
if array[mid] > target {
return binarySearch(array, target, lowIndex, mid)
} else if array[mid] < target {
return binarySearch(array, target, mid+1, highIndex)
} else {
return mid
}
}
使用循环实现二分查找
func iterbinarySearch(array []int, target int, lowIndex int, highIndex int) int {
startIndex := lowIndex
endIndex := highIndex
var mid int
for startIndex < endIndex {
mid = int((lowIndex + highIndex) / 2)
if array[mid] > target {
return binarySearch(array, target, lowIndex, mid)
} else if array[mid] < target {
return binarySearch(array, target, mid+1, highIndex)
} else {
return mid
}
}
return -1
}
英文:
Logic for Binary Search
- Target is to search for an item in an array.
- Get the middle item of the array.
- If more than the desired value => First item till the end item.
- If less than the desired value => Middle item till the end item
- Repeat process
We implement this using recursion or a loop.
Using recursion to make a binary search
Function will include an array where we will do a search. Then target value is equal to the value we are searching for.<br>
lowIndex will indicate the begining of our search.<br>
highIndex indicates the last position of our search.<br>
Then the function returns the position of the target value we are searching for.<br>
The reason for including the lowIndex and highIndex in the arguments is to search a subset of the array.
func binarySearch(array []int, target int, lowIndex int, highIndex int) int {
//specify condition to end the recursion
if highIndex < lowIndex {
return -1
}
// Define our middle index
mid := int((lowIndex + highIndex) / 2)
if array[mid] > target {
return binarySearch(array, target, lowIndex,mid)
}else if array[mid] < target {
return binarySearch(array, target,mid+1,highIndex)
}else {
return mid
}
}
Using a loop to make a binary search
func iterbinarySearch(array []int, target int, lowIndex int, highIndex int) int {
startIndex := lowIndex
endIndex := highIndex
var mid int
for startIndex < endIndex {
mid = int((lowIndex + highIndex) / 2)
if array[mid] > target {
return binarySearch(array, target, lowIndex, mid)
} else if array[mid] < target {
return binarySearch(array, target, mid+1, highIndex)
} else {
return mid
}
}
return -1
}
答案3
得分: 0
这是要翻译的内容:
http://play.golang.org/p/BbL-y7pJMi
据我所知,这个代码看起来运行正常。
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论