排序&二分查找
package main
import (
"fmt"
"sort"
)
var nums = []int{6, 42, 45, 81, 85, 42, 40, 57, 8, 1}
var cs = []string{"HA", "mmo0", "shSEpJ", "4C", "DKaHg", "B", "KI", "G", "Pnf", "2k8eQ"}
var persons = []Person{{"zeng qiang", 28}, {"xiao ming", 18}, {"yu wei", 36}}
func main() {
sort.Ints(nums) // 升序
fmt.Println("nums asc", nums)
keyword1 := 57
i := sort.SearchInts(nums, 57) // 二分法查找元素(仅升序)
fmt.Println(keyword1, "in", i)
sort.Sort(sort.Reverse(sort.IntSlice(nums))) // 降序
fmt.Println("nums desc", nums)
fmt.Println()
sort.Strings(cs) // 升序
fmt.Println("cs asc", cs)
keyword2 := "4C"
i = sort.SearchStrings(cs, keyword2)
fmt.Println(keyword2, "in", i)
sort.SliceStable(cs, func(i, j int) bool { // 按字符串最后一个字符升序
return cs[i][len(cs[i])-1] < cs[j][len(cs[j])-1]
})
fmt.Println(cs)
fmt.Println()
sort.SliceStable(persons, func(i, j int) bool { // 结构体按年龄升序
return persons[i].Age < persons[j].Age
})
fmt.Println("persons age asc", persons)
age := 28 // 按年龄查询下标
i = sort.Search(len(persons), func(i int) bool { return persons[i].Age >= age }) // 二分查找结构体
fmt.Println(age, "in", i)
sort.SliceStable(persons, func(i, j int) bool { // 结构体按名称(字符串)升序
return persons[i].Name < persons[j].Name
})
fmt.Println("persons name asc", persons)
name := "zeng qiang" // 按名字(字符串)查询下标
i = sort.Search(len(persons), func(i int) bool { return persons[i].Name >= name }) // 二分查找结构体
fmt.Println(name, "in", i)
}
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
}
生成数组
在线1:https://play.golang.org/
在线2:https://play.studygolang.com/
package main
import (
"encoding/json"
"fmt"
"math/rand"
"strings"
"time"
)
//在线1:https://play.golang.org/
//在线2:https://play.studygolang.com/
func init() {
t := time.Now().UnixNano()
rand.Seed(t)
}
func main() {
ints(10) // 生成整数数组
chars(10) // 生成字符串数组
}
func ints(length int) {
if length <= 0 {
length = 10
}
var arr []int
for i := 0; i < length; i++ {
arr = append(arr, rand.Intn(100))
}
s, _ := json.Marshal(arr)
nums := strings.Trim(string(s), "[]")
fmt.Printf("%s{%s}\n", "var arr = []int", nums)
}
func chars(length int) {
if length <= 0 {
length = 10
}
var arr []string
for i := 0; i < length; i++ {
l := 1
t := rand.Intn(6)
if t > 0 {
l = t
}
arr = append(arr, monoStr(l))
}
for i := 0; i < len(arr); i++ {
arr[i] = fmt.Sprintf("\"%s\"", arr[i])
}
fmt.Printf("%s{%s}\n", "var chars = []string", strings.Join(arr, ","))
}
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
const (
letterIdxBits = 6 // 6 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
)
var src = rand.NewSource(time.Now().UnixNano())
func monoStr(n int) string {
b := make([]byte, n)
for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = src.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
b[i] = letterBytes[idx]
i--
}
cache >>= letterIdxBits
remain--
}
return string(b)
}