前端之家收集整理的这篇文章主要介绍了
Golang二分查找算法的简单实现,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package main
import (
"fmt"
)
type Searchable interface {
Len() int
Less(int,int) bool
Equal(int,interface{}) bool
}
type List []int
func (l List) Len() int {
return len(l)
}
func (l List) Less(first int,second int) bool {
if l[first] < l[second] {
return true
}
return false
}
func (l List) Equal(index int,item interface{}) bool {
if value,ok := item.(int); ok {
if l[index] == value {
return true
}
}
return false
}
func main() {
list := []int{1,2,3,5,9}
index := binSearch(list,3)
fmt.Printf("The index of 3 in the list is %d\n",index)
index = binSearch(list,4)
fmt.Printf("The index of 4 in the list is %d\n",index)
}
func binSearch(list List,item interface{}) int {
startFlag := 0
stopFlag := list.Len() - 1
middleFlag := (startFlag + stopFlag) / 2
for (!list.Equal(middleFlag,item)) && (startFlag < stopFlag) {
if list.Less(startFlag,stopFlag) {
startFlag = middleFlag + 1
} else {
stopFlag = middleFlag - 1
}
middleFlag = (startFlag + stopFlag) / 2
}
if list.Equal(middleFlag,item) {
return middleFlag
} else {
return -1
}
}