-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheapsort.go
81 lines (68 loc) · 1.51 KB
/
heapsort.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
package main
import "fmt"
func heapify(seq []int, iRoot int, heapSize int) {
iMax := iRoot
iLeft := iRoot*2 + 1
iRight := iRoot*2 + 2
if (iLeft < heapSize) && (seq[iLeft] > seq[iMax]) {
iMax = iLeft
}
if (iRight < heapSize) && (seq[iRight] > seq[iMax]) {
iMax = iRight
}
if iMax != iRoot {
seq[iRoot], seq[iMax] = seq[iMax], seq[iRoot]
heapify(seq, iMax, heapSize)
}
}
func buildMaxHeap(seq []int, heapSize int) {
for i := heapSize / 2; i > 0; i-- {
heapify(seq, i-1, heapSize)
}
}
func heapSort(seq []int) {
n := len(seq)
buildMaxHeap(seq, n)
for i := n - 1; i > 0; i-- {
seq[0], seq[i] = seq[i], seq[0]
heapify(seq, 0, i)
}
}
func heapSortTest() {
isSorted := func(seq []int) bool {
for i := 0; i < len(seq)-1; i++ {
if seq[i] > seq[i+1] {
return false
}
}
return true
}
// Empty
seq := []int{}
fmt.Printf("test_empty: ")
heapSort(seq)
if isSorted(seq) {
fmt.Printf("passed: %v\n", seq)
}
// +
seq = []int{16, 7, 9, 5, 65, 49, 37, 3, 28, 2, 21, 12, 4}
fmt.Printf("test_int_1: ")
heapSort(seq)
if isSorted(seq) {
fmt.Printf("passed: %v\n", seq)
}
// +, -
seq = []int{-16, 7, -9, 5, -65, 49, -37, 3, -28, 2, -21, 12, -4}
fmt.Printf("test_int_2: ")
heapSort(seq)
if isSorted(seq) {
fmt.Printf("passed: %v\n", seq)
}
// +, -, =
seq = []int{1, 2, -4, -7, 1, 9, -7, 2, 2, 6, 1, 12, 4}
fmt.Printf("test_int_3: ")
heapSort(seq)
if isSorted(seq) {
fmt.Printf("passed: %v\n", seq)
}
}