企业🤖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) } ~~~