func merge(left,right []int) (result []int) {
r,l := 0,0
for l < len(left) && r < len(right) {
if left[l] < right[r]{
result = append(result,left[l])
l++
}else {
result = append(result,right[r])
r++
}
}
result = append(result,left[l:]...)
result = append(result,right[r:]...)
return
}
// 归并
func merget_sort(arr []int) []int {
if len(arr) <= 1{
return arr
}
mid := len(arr) / 2
left := merget_sort(arr[:mid])
right := merget_sort(arr[mid:])
return merge(left,right)
}
// 冒泡
func bubble_sort(arr []int) []int {
if len(arr) <= 1{
return arr
}
for i:=0;i<len(arr);i++{
for j:=0;j<len(arr)-i-1;j++{
if arr[j] > arr[j+1]{
arr[j],arr[j+1] = arr[j+1],arr[j]
}
}
}
return arr
}
// 快速
func quick_sort(arr []int) []int {
if len(arr)<=1{
return arr
}
mid,i := arr[0],1
head,tail := 0,len(arr)-1
for head < tail{
if arr[i] > mid {
arr[tail],arr[i] = arr[i],arr[tail]
tail--
}else {
arr[head],arr[i] = arr[i],arr[head]
head++
i++
}
}
arr[head] = mid
quick_sort(arr[:head])
quick_sort(arr[head+1:])
return arr
}
// 选择
func select_sort(arr []int)[]int{
if len(arr)<=1{
return arr
}
for i:=0;i<len(arr);i++{
c := i
for j:=i;j<len(arr);j++{
if arr[j] < arr[c] {
c = j
}
}
arr[c],arr[i] = arr[i],arr[c]
}
return arr
}
// 插入
func insert_sort(arr []int)[]int{
if len(arr)<=1{
return arr
}
for i:=1;i<len(arr);i++{
c := i
for c > 0 {
if arr[c] < arr[c-1]{
arr[c],arr[c-1] =arr[c-1],arr[c]
}
c--
}
}
return arr
}