mirror of
https://github.com/clockworkpi/LauncherGoDev.git
synced 2025-12-12 16:08:52 +01:00
38 lines
644 B
Go
38 lines
644 B
Go
package qsort
|
|
|
|
import "math/rand"
|
|
|
|
func QuickSort(slice []int) []int {
|
|
length := len(slice)
|
|
|
|
if length <= 1 {
|
|
sliceCopy := make([]int, length)
|
|
copy(sliceCopy, slice)
|
|
return sliceCopy
|
|
}
|
|
|
|
m := slice[rand.Intn(length)]
|
|
|
|
less := make([]int, 0, length)
|
|
middle := make([]int, 0, length)
|
|
more := make([]int, 0, length)
|
|
|
|
for _, item := range slice {
|
|
switch {
|
|
case item < m:
|
|
less = append(less, item)
|
|
case item == m:
|
|
middle = append(middle, item)
|
|
case item > m:
|
|
more = append(more, item)
|
|
}
|
|
}
|
|
|
|
less, more = QuickSort(less), QuickSort(more)
|
|
|
|
less = append(less, middle...)
|
|
less = append(less, more...)
|
|
|
|
return less
|
|
}
|