MCPcopy Create free account
hub / github.com/0xAX/go-algorithms / quick_sort

Function quick_sort

sorting/quick_sort.go:9–39  ·  view source on GitHub ↗
(arr []int)

Source from the content-addressed store, hash-verified

7import "math/rand"
8
9func quick_sort(arr []int) []int {
10
11 if len(arr) <= 1 {
12 return arr
13 }
14
15 median := arr[rand.Intn(len(arr))]
16
17 low_part := make([]int, 0, len(arr))
18 high_part := make([]int, 0, len(arr))
19 middle_part := make([]int, 0, len(arr))
20
21 for _, item := range arr {
22 switch {
23 case item < median:
24 low_part = append(low_part, item)
25 case item == median:
26 middle_part = append(middle_part, item)
27 case item > median:
28 high_part = append(high_part, item)
29 }
30 }
31
32 low_part = quick_sort(low_part)
33 high_part = quick_sort(high_part)
34
35 low_part = append(low_part, middle_part...)
36 low_part = append(low_part, high_part...)
37
38 return low_part
39}
40
41//func main() {
42// arr := utils.RandArray(10)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected