Skip to content

Commit 982e21f

Browse files
committed
feat: add heap sort
1 parent 9aac648 commit 982e21f

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1188,7 +1188,7 @@ list.sort(Comparator.comparingInt(String::length));
11881188
| **Worst** | $O(n + k)$ | when the input elements have a large range of values |
11891189
| **Average** | $O(n + k)$ | when the elements are distributed randomly in the array |
11901190

1191-
- Heap sort, CLRS#6: [java](/java-algorithm/src/main/java/com/example/algorithm/sort/HeapSort.java) | Heap sort is a comparison-based sorting algorithm that uses a binary heap data structure to sort an array. It is used for the implementation of priority queue.<br>($\textit{n}$ is the number of elements)
1191+
- Heap sort, CLRS#6: [golang](go-algorithm/pkg/sort/heap_sort.go), [java](/java-algorithm/src/main/java/com/example/algorithm/sort/HeapSort.java) | Heap sort is a comparison-based sorting algorithm that uses a binary heap data structure to sort an array. It is used for the implementation of priority queue.<br>($\textit{n}$ is the number of elements)
11921192

11931193
| **Case** | **Time complexity** | **Remarks** |
11941194
| ----------- | :-----------------: | ----------- |

go-algorithm/pkg/sort/heap_sort.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package sort
2+
3+
import (
4+
"golang.org/x/exp/constraints"
5+
)
6+
7+
func heapSort[T constraints.Integer](arr []T) {
8+
n := len(arr)
9+
_buildMaxHeap(arr)
10+
for i := n - 1; i > 0; i-- {
11+
arr[0], arr[i] = arr[i], arr[0]
12+
_maxHeapify(arr, 0, i)
13+
}
14+
}
15+
16+
func _buildMaxHeap[T constraints.Integer](arr []T) {
17+
n := len(arr)
18+
for i := n/2 - 1; i >= 0; i-- {
19+
_maxHeapify(arr, i, n)
20+
}
21+
}
22+
23+
func _maxHeapify[T constraints.Integer](arr []T, i, n int) {
24+
largest := i
25+
for {
26+
left := 2*i + 1
27+
right := 2*i + 2
28+
29+
if left < n && arr[largest] < arr[left] {
30+
largest = left
31+
}
32+
if right < n && arr[largest] < arr[right] {
33+
largest = right
34+
}
35+
if largest == i {
36+
break
37+
}
38+
39+
arr[i], arr[largest] = arr[largest], arr[i]
40+
i = largest
41+
}
42+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package sort
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"golang.org/x/exp/constraints"
8+
)
9+
10+
func Test_heapSort(t *testing.T) {
11+
type args[T constraints.Integer] struct {
12+
arr []T
13+
}
14+
type testCase[T constraints.Integer] struct {
15+
name string
16+
args args[T]
17+
want []T
18+
}
19+
tests := []testCase[int]{
20+
{
21+
name: "Integer Case",
22+
args: args[int]{
23+
arr: []int{31, 64, 49, 85, 71, 26, 6, 19},
24+
},
25+
want: []int{6, 19, 26, 31, 49, 64, 71, 85},
26+
},
27+
{
28+
name: "Empty array input",
29+
args: args[int]{},
30+
want: nil,
31+
},
32+
}
33+
for _, tt := range tests {
34+
t.Run(tt.name, func(t *testing.T) {
35+
heapSort(tt.args.arr)
36+
37+
assert.Equalf(t, tt.want, tt.args.arr, "heapSort() got = %v, want %v", tt.args.arr, tt.want)
38+
})
39+
}
40+
}

0 commit comments

Comments
 (0)