-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc.go
73 lines (66 loc) · 1.05 KB
/
misc.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
68
69
70
71
72
73
package main
import(
"fmt"
)
//tw
/**
* This file is just for me to try out stuff
*/
func mergeSort(a *[]int, ta *[]int, l int, r int) int{
if l == r {
return l
} else {
m := int((l + r)/2)
l := mergeSort(a, ta, l, m)
r := mergeSort(a, ta, m+1, r)
merger(a, ta, l, m + 1, r + 1)
return l
}
}
func merger(a *[]int, ta *[]int, l int, r int, e int) {
t := l
left := l
right := r
for left < r && right < e {
if (*a)[left] < (*a)[right] {
(*ta)[t] = (*a)[left]
left++
} else {
(*ta)[t] = (*a)[right]
right++
}
t++
}
for left < r {
(*ta)[t] = (*a)[left]
left++
t++
}
for right < e {
(*ta)[t] = (*a)[right]
right++
t++
}
for i := l; i < e; i++ {
(*a)[i] = (*ta)[i]
}
}
func countTo1000(n chan int) {
for i := 0; i < 1000; i++ {
fmt.Println(i)
n <- 1
}
}
func main() {
n := make(chan int, 1000);
a := []int{4,1,-1,4,7,-3,9}
t := make([]int, len(a))
mergeSort(&a, &t, 0, len(a) - 1)
fmt.Println(a)
go countTo1000(n)
sum := 0
for i := 0; i < 1000; i += <- n {
sum += 1
}
fmt.Println("sum: ", sum)
}