🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
> # 归并排序 ~~~ package main import "fmt" func mergeSort(arr []int) []int { if len(arr) <= 1 { return arr } // 分割数组 middle := len(arr) / 2 left := mergeSort(arr[:middle]) right := mergeSort(arr[middle:]) // 合并数组 return merge(left, right) } func merge(left, right []int) []int { result := make([]int, 0, len(left)+len(right)) l, r := 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 result } func main() { // 测试数据 arr := []int{64, 34, 25, 12, 22, 11, 90} fmt.Println("排序前:", arr) arr = mergeSort(arr) fmt.Println("排序后:", arr) } ~~~