This repository has been archived by the owner on Jun 24, 2021. It is now read-only.
forked from hoanhan101/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheapsort_test.go
107 lines (87 loc) · 2.21 KB
/
heapsort_test.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
Problem:
- Implement heapsort.
Approach:
- Similar to selection sort, repeatedly choose the largest item and move it to
the end of the array using a max heap.
Cost:
- O(nlogn) time and O(1) space.
*/
package lab
import (
"testing"
"github.com/hoanhan101/algo/common"
)
func TestHeapsort(t *testing.T) {
tests := []struct {
in []int
expected []int
}{
{[]int{}, []int{}},
{[]int{1}, []int{1}},
{[]int{1, 2}, []int{1, 2}},
{[]int{2, 1}, []int{1, 2}},
{[]int{2, 1, 3}, []int{1, 2, 3}},
{[]int{1, 1, 1}, []int{1, 1, 1}},
{[]int{2, 1, 2}, []int{1, 2, 2}},
{[]int{1, 2, 4, 3, 6, 5}, []int{1, 2, 3, 4, 5, 6}},
{[]int{6, 2, 4, 3, 1, 5}, []int{1, 2, 3, 4, 5, 6}},
{[]int{6, 5, 4, 3, 2, 1}, []int{1, 2, 3, 4, 5, 6}},
}
for _, tt := range tests {
heapsort(tt.in)
common.Equal(t, tt.expected, tt.in)
}
}
func heapsort(in []int) {
heapify(in)
size := len(in)
for size > 0 {
// repeatedly remove the largest item.
largest := removeLargest(in, size)
// update the heap size.
size--
// store the removed value at the end of the list.
in[size] = largest
}
}
// heapify transform the input into a max heap.
func heapify(in []int) {
for i := len(in) - 1; i > -1; i-- {
bubbleDown(in, len(in), i)
}
}
// bubbleDown allow larger values to reach the top.
func bubbleDown(heap []int, heapSize int, index int) {
for index < heapSize {
// fast-calculate the children left and right index.
left := index*2 + 1
right := index*2 + 2
// stop if there is no child node.
if left >= heapSize {
break
}
// find the larger index
larger := left
if right < heapSize && heap[left] < heap[right] {
larger = right
}
// if the current item is larger than both children, we're done.
// if not, swap with the larger child.
if heap[index] < heap[larger] {
common.Swap(heap, index, larger)
} else {
break
}
}
}
// removeLargest remove and return the largest item from the heap.
func removeLargest(heap []int, heapSize int) int {
// largest item is at the top of our max heap.
largest := heap[0]
// move the last item into the root position.
heap[0] = heap[heapSize-1]
// bubble down from the root to restore the heap.
bubbleDown(heap, heapSize-1, 0)
return largest
}