generated from alura/modelo-vitrinedev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheapsort.go
81 lines (67 loc) · 1.53 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 sort
type Heap[T any] struct {
array []T
length int
}
func HeapSort[T any](array []T, less func(T, T) bool) {
if len(array) <= 1 {
return
}
var h Heap[T] = Heap[T]{array: (array), length: len(array)}
var end int = len(array) - 1
for i := h.parent(end); i >= 0; i-- {
h.Heapify(i, less)
}
for i := end; i > 0; i-- {
h.swap(i, 0)
h.length--
h.Heapify(0, less)
}
}
func (h Heap[T]) Heapify(index int, less func(T, T) bool) {
left, left_exists := h.leftChild(index)
right, right_exists := h.rightChild(index)
greater := index
if left_exists && less(h.get(greater), h.get(left)) {
greater = left
}
if right_exists && less(h.get(greater), h.get(right)) {
greater = right
}
if greater != index {
h.swap(index, greater)
h.Heapify(greater, less)
}
}
func (h Heap[T]) get(index int) T {
return h.array[index]
}
func (h Heap[T]) swap(index1, index2 int) {
h.array[index1], h.array[index2] = h.array[index2], h.array[index1]
}
func (h Heap[T]) parent(index int) (parentIndex int) {
if index <= 0 {
parentIndex = 0
} else {
parentIndex = (index - 1) / 2
}
return
}
func (h Heap[T]) leftChild(index int) (leftChildIndex int, leftChildExists bool) {
if index < 0 {
leftChildIndex = 0
} else {
leftChildIndex = index*2 + 1
}
leftChildExists = leftChildIndex < h.length
return
}
func (h Heap[T]) rightChild(index int) (rightChildIndex int, rightChildExists bool) {
if index < 0 {
rightChildIndex = 0
} else {
rightChildIndex = index*2 + 2
}
rightChildExists = rightChildIndex < h.length
return
}