-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort.go
56 lines (50 loc) · 1.01 KB
/
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
package codility
//https://en.wikipedia.org/wiki/Sorting_algorithm#
// Recursive. Top down
func MergeSort(A []int) {
B := make([]int, len(A)) // Temp
mergeSortRecursive(A, B)
}
func mergeSortRecursive(A, B []int) {
n := len(A)
if n > 1 {
splitMiddle := n / 2
part1 := A[:splitMiddle]
part2 := A[splitMiddle:]
mergeSortRecursive(part1, B[:splitMiddle])
mergeSortRecursive(part2, B[splitMiddle:])
// Merging to result
i1, i2 := 0, 0
for i := 0; i < n; i++ {
if i1 < splitMiddle && (i2 >= n-splitMiddle || part1[i1] < part2[i2]) {
B[i] = part1[i1]
i1++
} else {
B[i] = part2[i2]
i2++
}
}
for i := 0; i < n; i++ {
A[i] = B[i]
}
}
}
func InsertSort(A []int) {
n := len(A)
for i := 1; i < n; i++ {
for j := i - 1; j >= 0 && A[j] > A[j+1]; j-- {
A[j], A[j+1] = A[j+1], A[j]
}
}
}
func SelectionSort(A []int) {
for i := 0; i < len(A); i++ {
minI := i
for j := i + 1; j < len(A); j++ {
if A[j] < A[minI] {
minI = j
}
}
A[i], A[minI] = A[minI], A[i]
}
}