-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitonic_sort.go
113 lines (96 loc) · 2.24 KB
/
bitonic_sort.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import
(
"fmt"
"time"
"math/rand"
)
const (
ASC bool = true
DESC bool = false
)
var (
ARR_SIZE int = 1<<7
order bool = DESC
)
func init() {
rand.Seed(time.Now().UTC().UnixNano())
}
func bitonic_sort_go(arr []int, orderby bool){
breakchan := make(chan int)
go bitonic_sort(arr, breakchan, orderby)
<-breakchan
}
func bitonic_sort(arr []int ,breakchan chan int, orderby bool){
if len(arr) < 2 {
breakchan <- 1
return
}
middle := len(arr) / 2
c1 := make(chan int)
c2 := make(chan int)
go bitonic_sort(arr[:middle], c1, ASC)
go bitonic_sort(arr[middle:], c2, DESC)
<-c1
<-c2
bitonic_merge(arr, breakchan, orderby)
}
func bitonic_compare(arr []int, orderby bool){
middle := len(arr) / 2
for i := 0; i < middle; i++ {
if (arr[i]>arr[i+middle]) == orderby {
arr[i], arr[i+middle] = arr[i+middle], arr[i]
}
}
}
func bitonic_merge(arr []int, breakchan chan int, orderby bool){
bitonic_compare(arr, orderby)
middle := len(arr) / 2
if middle > 1 {
c1 := make(chan int)
c2 := make(chan int)
go bitonic_merge(arr[:middle], c1, orderby)
go bitonic_merge(arr[middle:], c2, orderby)
<-c1
<-c2
}
breakchan <- 1
}
func randInt(min int, max int) int {
return min+rand.Intn(max-min)
}
func test(orderby bool, arr []int) bool {
if len(arr) < 2 {
return true
}
prev := arr[0]
for _, n := range arr[1:]{
if orderby {
if prev > n {
fmt.Println("ASC" ,prev, n)
return false
}
} else
if prev < n {
fmt.Println("DESC",prev, n)
return false
}
prev = n
}
return true
}
func main(){
my_arr := make([]int, ARR_SIZE)
for i := 0; i < ARR_SIZE; i++ {
my_arr[i] = randInt(1,ARR_SIZE/2)
}
bitonic_sort_go(my_arr, order)
fmt.Println("ARR_SIZE:", ARR_SIZE)
if order {
fmt.Println("Order By ASC")
} else {
fmt.Println("Order By DESC")
}
fmt.Println("my_arr after bitonic sort: ", my_arr)
fmt.Println("Test: ", test(order, my_arr))
}