-
Notifications
You must be signed in to change notification settings - Fork 0
/
quick.go
67 lines (54 loc) · 1.36 KB
/
quick.go
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"fmt"
"math/rand"
"time"
)
const DATASET_LEN = 100000000
const DATASET_RANGE = 1000000
func gen_dataset() []int {
arr := [DATASET_LEN]int{}
for i := 0; i < DATASET_LEN; i++ {
arr[i] = rand.Intn(DATASET_RANGE)
}
return arr[:]
}
func quick(dataset []int) chan struct{} {
done := make(chan struct{})
if len(dataset) <= 1 {
close(done)
return done
}
go func() {
defer close(done)
p1 := 0
p2 := len(dataset) - 1
for p1 != p2 {
if (dataset[p1] > dataset[p2] && p1 < p2) || (dataset[p1] < dataset[p2] && p1 > p2) {
dataset[p1], dataset[p2] = dataset[p2], dataset[p1]
p1, p2 = p2, p1
}
if p1 < p2 {
p1 += 1
} else {
p1 -= 1
}
}
doneleft := quick(dataset[0:p1])
<-quick(dataset[p1+1:])
<-doneleft
}()
return done
}
func main() {
dataset := gen_dataset()
//fmt.Printf("DATASET: %v\n\n", dataset)
startt := time.Now()
<-quick(dataset)
endt := time.Now()
//fmt.Printf("SORTED DATASET: %v\n\n", dataset)
fmt.Printf("Sorted %d values ranging from 0 to %d in %s\n", DATASET_LEN, DATASET_RANGE, endt.Sub(startt).String())
}
func init() {
rand.Seed(time.Now().UnixNano())
}