-
Notifications
You must be signed in to change notification settings - Fork 1
/
sort_go_routines.go
77 lines (58 loc) · 1.12 KB
/
sort_go_routines.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
package main
import (
"fmt"
"sort"
"strconv"
)
func sortArray(s []int, c chan []int) {
defer close(c)
sort.Slice(s, func(i, j int) bool {
return s[i] < s[j]
})
c <- s
}
func main() {
var s string
fmt.Println("Enter the size of the input array:")
fmt.Scan(&s)
si, err := strconv.ParseInt(s, 10, 64)
size := int(si)
if err != nil {
fmt.Println("Please enter Integer. Exiting...")
return
}
slice := make([]int, 0, size)
fmt.Println("Enter the numbers to sort: ")
var number string
var m int
for m = 0; m < size; m++ {
fmt.Scan(&number)
n, err := strconv.ParseInt(number, 10, 64)
num := int(n)
if err != nil {
fmt.Println("Please enter Integer. Exiting...")
return
}
slice = append(slice, num)
}
o := 0
l := (size / 4)
tmp := make([]int, 0, size)
result := make([]int, 0, size)
for i := 0; i < 4; i++ {
c := make(chan []int)
if i == 3 {
tmp = slice[o:size]
} else {
tmp = slice[o:(o + l)]
}
go sortArray(tmp, c)
for res := range c {
result = append(result, append(res)...)
}
o += l
}
c := make(chan []int)
go sortArray(result, c)
fmt.Println(<-c)
}