-
Notifications
You must be signed in to change notification settings - Fork 0
/
heapsort.go
54 lines (48 loc) · 1.15 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
package sort
import (
"github.com/MarkWh1te/algo4go/util"
)
// MaxHeapInsert keeps max heap and insert element into Maxheap
func MaxHeapInsert(arr *[]int, index int) {
for (*arr)[index] > (*arr)[(index-1)/2] {
util.Swap(&((*arr)[index]), &((*arr)[(index-1)/2]))
index = (index - 1) / 2
}
}
// MaxHeapify assumes that the binary trees rooted at LEFT and RIGHT are maxheaps
func MaxHeapify(arr *[]int, index int, size int) {
var largest int
left := index*2 + 1
right := index*2 + 2
for left < size {
largest = index
if (*arr)[left] > (*arr)[index] {
largest = left
}
if right < size && (*arr)[right] > (*arr)[largest] {
largest = right
}
// if largest is index means down up to righ place,termiate!
if largest == index {
return
}
util.Swap(&((*arr)[largest]), &((*arr)[index]))
index = largest
left = index*2 + 1
right = index*2 + 2
}
}
// HeapSort runs in O(nutil.Log(n)) but cache poorly
func HeapSort(arr *[]int) {
size := len(*arr)
if len(*arr) < 2 {
return
}
for i := 0; i < size; i++ {
MaxHeapInsert(arr, i)
}
for n := size - 1; n >= 0; n-- {
util.Swap(&((*arr)[0]), &((*arr)[n]))
MaxHeapify(arr, 0, n)
}
}