1. 经典排序
1.1. 归并排序
显示代码
func mergeSort(nums []int) []int {
length := len(nums)
if length < 2 {
return nums
}
middle := length / 2
left := nums[0:middle]
right := nums[middle:]
return merge(mergeSort(left), mergeSort(right))
}
func merge(left []int, right []int) []int {
var result []int
for len(left) != 0 && len(right) != 0 {
if left[0] <= right[0] {
result = append(result, left[0])
left = left[1:]
} else {
result = append(result, right[0])
right = right[1:]
}
}
//fmt.Println(left,right)
for len(left) != 0 {
result = append(result, left[0])
left = left[1:]
}
for len(right) != 0 {
result = append(result, right[0])
right = right[1:]
}
return result
}