# golang快速排序

``````package main

import "fmt"

func main() {
numbers := []int{6, 2, 7, 7, 3, 8, 9}
QuickSort(numbers)
fmt.Println(numbers)
}

func QuickSort(values []int) []int {
length := len(values)
if length <= 1 {
return values
}

mid := values[0]
left := make([]int,0)
right := make([]int,0)
for i:=1;i<len(values);i++{
if values[i]<mid{
left = append(left, values[i])
}else{
right = append(right, values[i])
}
}

// 分水岭左右的元素递归做同样处理
left = QuickSort(left)
right = QuickSort(right)

return append(append(left, mid), right...)
}
``````

0 回复

• 请尽量让自己的回复能够对别人有帮助
• 支持 Markdown 格式, **粗体**、~~删除线~~、``单行代码``
• 支持 @ 本站用户；支持表情（输入 : 提示），见 Emoji cheat sheet
• 图片支持拖拽、截图粘贴等方式上传
``````package main

import "fmt"

func main() {
numbers := []int{6, 2, 7, 7, 3, 8, 9}
QuickSort(numbers)
fmt.Println(numbers)
}

func QuickSort(values []int) []int {
length := len(values)
if length <= 1 {
return values
}

mid := values[0]
left := make([]int,0)
right := make([]int,0)
for i:=1;i<len(values);i++{
if values[i]<mid{
left = append(left, values[i])
}else{
right = append(right, values[i])
}
}

// 分水岭左右的元素递归做同样处理
left = QuickSort(left)
right = QuickSort(right)

return append(append(left, mid), right...)
}
``````