-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergesort.go
92 lines (83 loc) · 1.68 KB
/
mergesort.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
package main
import (
"fmt"
"math/rand"
"time"
)
type customStruct struct {
stack [][]int
size int
}
func LRandint(min int, max int) int {
return min + rand.Intn(max-min)
}
func sise(a []int) int {
return len(a)
}
func PushFront(size, value int) [][]int {
c := customStruct{size: size}
m := make([][]int, c.size)
for i := 0; i < len(m); i++ {
t := LRandint(1, len(m))
for j := 0; j < t; j++ {
m[i] = append(m[i], value)
} //create massive
if len(m) > 1 {
for h := 0; h < len(m); h++ {
if len(m[i]) < 1 {
m = append(m[:h], m[h+1:]...)
h--
} //reversed massive
}
}
}
return m
}
func mergen(a [][]int, b [][]int) [][]int { //double merge sort
final := make([][]int, 1)
i := 0
j := 0
for i < len(a) && j < len(b) {
if sise(a[i]) < sise(b[j]) {
final = append(final, a[i])
i++
} else {
final = append(final, b[j])
j++
}
}
for ; i < len(a); i++ {
final = append(final, a[i])
}
for ; j < len(b); j++ {
final = append(final, b[j])
}
return final
}
func mergeSorti(items [][]int) [][]int {
if len(items) < 2 {
return items
}
first := mergeSorti(items[:len(items)/2])
second := mergeSorti(items[len(items)/2:])
return mergen(first, second)
}
func main() {
var n int
fmt.Scanln(&n)
now := time.Now()
c := customStruct{stack: PushFront(n, 1), size: n}
fmt.Println(c.stack)
sorted := mergeSorti(c.stack)
for i := 0; i < len(sorted); i++ {
if len(sorted[i]) < 1 {
sorted = append(sorted[:i], sorted[i+1:]...)
i--
}
}
durat := time.Since(now)
fmt.Println(sorted)
fmt.Println(durat)
//TODO an OP как считать кол-во операций в разных функциях?
//TODO посчитать big O
}