-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread.go
More file actions
79 lines (66 loc) · 1.32 KB
/
thread.go
File metadata and controls
79 lines (66 loc) · 1.32 KB
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
package main
import (
"bufio"
"fmt"
"os"
"reflect"
"strconv"
"strings"
)
func main() {
var nums []int
fmt.Println("Enter a minumum of 8 space-separated integers")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
nums = numbers(scanner.Text())
firstHalf, secondHalf := split(nums)
firstSl, secondSl := split(firstHalf)
thirdSl, fourthSl := split(secondHalf)
sorted := []int{}
go BubbleSort(firstSl)
go BubbleSort(secondSl)
go BubbleSort(thirdSl)
go BubbleSort(fourthSl)
sorted = append(sorted, firstSl...)
sorted = append(sorted, secondSl...)
sorted = append(sorted, thirdSl...)
sorted = append(sorted, fourthSl...)
BubbleSort(sorted)
fmt.Println(sorted)
}
func numbers(s string) []int {
var n []int
for _, f := range strings.Fields(s) {
i, err := strconv.Atoi(f)
if err == nil {
n = append(n, i)
}
}
return n
}
func split(s []int) (x, y []int) {
first := s[:len(s)/2]
second := s[len(s)/2:]
return first, second
}
func BubbleSort(s []int) {
fmt.Println("Sorting ", s)
isSliceSorted := false
for !isSliceSorted {
swapped := false
for i := 0; i < len(s)-1; i++ {
if s[i] > s[i+1] {
Swap(s, i)
swapped = true
}
}
if !swapped {
isSliceSorted = true
}
}
fmt.Println("sorting is done")
}
func Swap(sl []int, i int) {
swapI := reflect.Swapper(sl)
swapI(i, i+1)
}