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...)
}
有疑问加站长微信联系(非本文作者)