快排
package main import ( "fmt" "math/rand" "time" ) func main() { li:=[]int{1,3,5,2,4,6,9,7} left:=0 right:=len(li)-1 fmt.Println(quick_sort(li,left,right)) } func quick_sort(li []int, left,right int) []int { if left=temp{ right-=1 } li[left]=li[right] for left
冒泡
package main import "fmt" func main() { li:=[]int{1,3,5,2,4,6,9,7} fmt.Println(bubble_sort(li)) } func bubble_sort(li[]int) []int { for i:=0;ili[j+1]{ li[j],li[j+1]=li[j+1],li[j] res=false } } if res{ return li } } return nil }
选择排序
package main import "fmt" func main() { li:=[]int{1,3,5,2,4,6,9,7} fmt.Println(select_sort(li)) } func select_sort(li[]int) []int { for i:=0;i
插入排序
package main import "fmt" func main() { li:=[]int{1,3,201,5,2,100,4,6,9,7,2} fmt.Println(insert_sort(li)) } func insert_sort(li[]int) []int { for i:=1;i0 && li[j]>tmp { li[j+1]=li[j] j=j-1 } li[j+1] = tmp } return li }
希尔排序
package main import "fmt" func main() { li:=[]int{1,3,201,5,2,100,4,6,9,7,2} fmt.Println(shell_sort(li)) } func shell_sort(li[]int) []int { res := len(li)/2 for res>0 { for i:=res;i=0 && tmp
二分法查找
package main import "fmt" func main() { li:=[]int{1,2,3,4,5,6,7,8} left:=0 right:=len(li)-1 value := 8 fmt.Println(bin_search(li,value,left,right)) } func bin_search(li[]int,value,left,right int) int { if left <=right{ mid := (left+right)/2 if li[mid] == value{ return mid } else if li[mid]>value { return bin_search(li,value,left,mid-1) } else { return bin_search(li,value,mid+1,right) } } else { return 999 } }
以上就是GO语言中常见的排序算法使用示例的详细内容,更多关于GO语言排序算法的资料请关注脚本之家其它相关文章!