forked from ayan-b/Quick-Sort
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.go
More file actions
39 lines (29 loc) · 677 Bytes
/
QuickSort.go
File metadata and controls
39 lines (29 loc) · 677 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//Code snippet of user vderyagin
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
}