-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_sort.go
100 lines (80 loc) · 2.21 KB
/
merge_sort.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
93
94
95
96
97
98
99
100
package mysorts
import (
"math"
)
/************************* In-place implementation *************************/
// MergeSortInPlace implements Merge Sort In-place
func MergeSortInPlace(in myInterface) {
mergeSortImpl(in, 0, in.Len()-1)
}
// mergeSortImpl split the array to two parts, then recurses call itself for them.
// after that merge results of the two parts.
func mergeSortImpl(in myInterface, p, r int) {
if p < r {
q := (p + r) / 2
mergeSortImpl(in, p, q)
mergeSortImpl(in, q+1, r)
merge(in, p, q, r)
}
}
//merge sorted sub-array in[p~q] and in[q+1~r] in-place, 0 <= p <= q < r= < in.Len()-1
func merge(in myInterface, p, q, r int) {
i := p
j := q + 1
for k := p; k < r; k++ {
if j > r {
break
}
if !in.Less(i, j) {
for m := j; m > i; m-- {
in.Swap(m, m-1)
}
j++
}
i++
}
}
/************************* In-place implementation *************************/
/************************* With aux array implementation *************************/
// MergeSort implements Merge Sort with aux array
func MergeSort(in []int) {
mergeSortImplAuxArrayBased(in, 0, len(in)-1)
}
// mergeSortImplAuxArrayBased split the array to two parts, then recurses call itself for them.
// after that merge results of the two parts.
// Same process flow as mergeSortImpl
func mergeSortImplAuxArrayBased(in []int, p, r int) {
if p < r {
q := (p + r) / 2
mergeSortImplAuxArrayBased(in, p, q)
mergeSortImplAuxArrayBased(in, q+1, r)
mergeAuxArrayBased(in, p, q, r)
}
}
func mergeAuxArrayBased(in []int, p, q, r int) {
n1 := q - p + 1
n2 := r - q // r - (q+1) + 1
// create aux arrays
auxArray1 := make([]int, n1+1, n1+1)
auxArray2 := make([]int, n2+1, n2+1)
for i := 0; i < n1; i++ {
auxArray1[i] = in[p+i]
}
auxArray1[n1] = math.MaxInt32 // set last value to infinity, only for aux
for i := 0; i < n2; i++ {
auxArray2[i] = in[q+1+i]
}
auxArray2[n2] = math.MaxInt32 // set last value to infinity, only for aux
// merge two aux arrays into original array
i, j := 0, 0
for k := p; k <= r; k++ {
if auxArray1[i] <= auxArray2[j] {
in[k] = auxArray1[i]
i++
} else {
in[k] = auxArray2[j]
j++
}
}
}
/************************* With aux array implementation *************************/