-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcurrentSort.go
63 lines (48 loc) · 1.14 KB
/
concurrentSort.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
package main
import (
"bufio"
"fmt"
"os"
"sort"
"strconv"
"strings"
"sync"
)
func main() {
fmt.Println("Please enter a list of integers separated with a white space")
br := bufio.NewReader(os.Stdin)
input, _, _ := br.ReadLine()
s := strings.Split(string(input), " ")
var numbers []int
for _, v := range s {
intV, _ := strconv.Atoi(v)
numbers = append(numbers, intV)
}
m := int64(len(numbers) / 4)
var wg = sync.WaitGroup{}
sortedSlices := make(map[int][]int)
wg.Add(4)
go sortSlice(numbers[0:m], sortedSlices, &wg)
go sortSlice(numbers[m:2*m], sortedSlices, &wg)
go sortSlice(numbers[2*m:3*m], sortedSlices, &wg)
go sortSlice(numbers[3*m:], sortedSlices, &wg)
wg.Wait()
sortMapAndPrint(sortedSlices)
}
func sortSlice(slice []int, sortedSlices map[int][]int, wg *sync.WaitGroup) {
sort.Ints(slice)
sortedSlices[slice[0]] = slice
wg.Done()
}
func sortMapAndPrint(mapToSort map[int][]int) {
slice := make([]int, 4)
var finalSlice []int
for k, _ := range mapToSort {
slice = append(slice, k)
}
sort.Ints(slice)
for _, v := range slice {
finalSlice = append(finalSlice, mapToSort[v]...)
}
fmt.Print(finalSlice)
}