顺序查找
package dataStructure
// go数据结构-顺序查找
import (
/"fmt"
math/rand"
strconv"
)
type Person struct {
ID int
Name string
}
// 构建静态顺序表
func generateStaticSearchTable(n int) []Person {
persons := make([]Person, n, n)
for i := 0; i < n; i++ {
ind := rand.Intn(100)
// persons = append(persons, Person{ind, "s" + strconv.Itoa(ind)})
persons[i] = Person{ind, "s" + strconv.Itoa(ind)}
}
return persons
}
// 顺序查找算法 复杂度O(n)
func sequentialSearch(persons []Person, id int) int {
n := len(persons)
n--
for n >= 0 && id != persons[n].ID {
n--
}
return n
}
/*
func main() {
persons := generateStaticSearchTable(10)
fmt.Println(len(persons), cap(persons))
for _, p := range persons {
fmt.Println(p.ID, p.Name)
}
index := sequentialSearch(persons, 81)
fmt.Println("person: ", index)
}
*/