algorithms-go/other/排序/快排/main.go
2021-04-07 14:27:14 +08:00

55 lines
960 B
Go

package main
import (
"fmt"
"math/rand"
)
func quick_sort(arr []int) []int {
if len(arr) <= 1 {
return arr
}
median := arr[rand.Intn(len(arr))]
low_part := make([]int, 0, len(arr))
high_part := make([]int, 0, len(arr))
middle_part := make([]int, 0, len(arr))
for _, item := range arr {
switch {
case item < median:
low_part = append(low_part, item)
case item == median:
middle_part = append(middle_part, item)
case item > median:
high_part = append(high_part, item)
}
}
low_part = quick_sort(low_part)
high_part = quick_sort(high_part)
low_part = append(low_part, middle_part...)
low_part = append(low_part, high_part...)
return low_part
}
func main() {
arr := RandomArray(10)
fmt.Println("Initial array is:", arr)
fmt.Println("")
fmt.Println("Sorted array is: ", quick_sort(arr))
}
func RandomArray(n int) []int {
arr := make([]int, n)
for i := 0; i <= n-1; i++ {
arr[i] = rand.Intn(n)
}
return arr
}