-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheapsort.go
More file actions
47 lines (40 loc) · 879 Bytes
/
heapsort.go
File metadata and controls
47 lines (40 loc) · 879 Bytes
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
package sorting
func left_child(index int) int {
return (2 * index) + 1
}
func right_child(index int) int {
return (2 * index) + 2
}
func do_max_heap(s []int32) {
i := (len(s) / 2) + 1
for ;i>=0;i-- {
max_heapify(s, i, 0)
}
}
func max_heapify(s []int32, index int, size_mod int) {
var max int
left := left_child(index)
right := right_child(index)
if left < len(s) - size_mod && s[index] < s[left] {
max = left
} else {
max = index
}
if right < len(s) - size_mod && s[max] < s[right] {
max = right
}
if max != index {
s[max], s[index] = s[index], s[max]
max_heapify(s, max, size_mod)
}
}
func heapsort(s []int32) {
do_max_heap(s)
i := len(s) - 1
j := 1
for ;i>0;i-- {
s[0], s[i] = s[i], s[0]
max_heapify(s, 0, j)
j++
}
}