1.快排核心概念
思路是使用递归,依此循环数据,不断把大小分成3类,与参考数比较分 大于,小于,等于
2.直接上代码
package main
import "fmt"
func main() {
arr := []int{2, 4, 2, 11, 42, 1, 32, 33, 12}
s := Quicksort(arr)
fmt.Print(s)
}
/*
1.找到递归终止条件
*/
func Quicksort(arr []int) []int {
length := len(arr)
if length <= 1 {
return arr
} else {
first := arr[0]
left := make([]int, 0)
right := make([]int, 0)
equit := make([]int, 0)
equit = append(equit, first)
for i := 1; i < length; i++ {
if arr[i] < first { // 左
left = append(left, arr[i])
} else if arr[i] > first {
right = append(right, arr[i])
} else {
equit = append(equit, arr[i])
}
}
//
low, height := Quicksort(left), Quicksort(right)
my := append(append(low, equit...), height...)
return my
}
}