package main
import "fmt"
func main() {
arr := []int{9,1,10,8,4,7,2,0,6,5,3}
result := QuickSort(arr)
fmt.Println(result)
}
func ComparisonNum(arr []int) []int {
if len(arr) == 0 {
return []int{}
}
left := []int{}
right := []int{}
nums := arr[len(arr) / 2]
for _,val := range arr {
if val < nums {
left = append(left, val)
} else {
right = append(right, val)
}
}
return MergeArray(left, right)
}
func QuickSort(arr []int) []int {
count := len(arr)
result := []int{}
if count / 2 < 2 {
result = ComparisonNum(arr)
} else {
nums := arr[len(arr) / 2]
left := []int{}
right := []int{}
for _,value := range arr {
if value < nums || value == nums {
left = append(left, value)
} else {
right = append(right, value)
}
}
leftResult := QuickSort(left)
rightResult := QuickSort(right)
result = MergeArray(leftResult, rightResult)
}
return result
}
func MergeArray(left, right []int) []int {
for _,val := range right {
left = append(left, val)
}
return left
}
有疑问加站长微信联系(非本文作者)