ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
> # 归并排序 ~~~ 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) } ~~~