-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathmergeSort.go
69 lines (65 loc) · 1.41 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
package sort
/*
merge sort O(nlgn):
T(n) = 2T(n/2) + O(n)
master theorem:
a = 2, b = 2, f(n) = n
logb(a) = lg2 = 1 f(n) = f(n^logb(a)) = f(n^1)
so, O(n) = O(n^logb(a)lgn) = O(nlgn)
*/
import (
"sync"
)
func merge(arr []int) {
i := len(arr) / 2
//copy left and right array
leftArr, rightArr := make([]int, i, i), make([]int, len(arr)-i, len(arr)-i)
copy(leftArr, arr[:i])
copy(rightArr, arr[i:])
leftIter, rightIter := ints(leftArr).Iter(), ints(rightArr).Iter()
leftValue, leftHasNext := leftIter()
rightValue, rightHasNext := rightIter()
//merge
for k := range arr {
if !leftHasNext { //left empty, use right value, in CLRS, use infinity
arr[k] = rightValue
rightValue, rightHasNext = rightIter()
} else if !rightHasNext { //right empty, use left value, in CLRS, use infinity
arr[k] = leftValue
leftValue, leftHasNext = leftIter()
} else {
if leftValue > rightValue {
arr[k] = rightValue
rightValue, rightHasNext = rightIter()
} else {
arr[k] = leftValue
leftValue, leftHasNext = leftIter()
}
}
}
}
func mergeSort(arr []int) {
i := len(arr) / 2
if i > 0 {
mergeSort(arr[:i])
mergeSort(arr[i:])
merge(arr)
}
}
func mergeSortParallel(arr []int) {
i := len(arr) / 2
if i > 0 {
var wd sync.WaitGroup
wd.Add(2)
go func() {
mergeSortParallel(arr[:i])
wd.Done()
}()
go func() {
mergeSortParallel(arr[i:])
wd.Done()
}()
wd.Wait()
merge(arr)
}
}