moved heapSort to its own file
Go / build (push) Successful in 26s

This commit is contained in:
Acid
2026-07-24 03:57:15 -04:00
parent 5fb97bc757
commit 35592e1bb8
3 changed files with 167 additions and 25 deletions
+30
View File
@@ -0,0 +1,30 @@
package heap
import (
"cmp"
)
// HeapSort() -> takes in an array and returns it sorted. O(n log n)
func HeapSort[T cmp.Ordered](arg []T) []T {
if len(arg) <= 0 {
return nil
}
heap := NewMinHeap[T]()
heap.Heapify(arg)
sorted := make([]T, 0, len(arg))
for range heap.Array {
val, ok := heap.PopMin()
if !ok {
break
}
sorted = append(sorted, val)
}
return sorted
}
@@ -4,31 +4,6 @@ import (
"cmp"
)
// HeapSrort() -> takes in an array and returns it sorted. O(n log n)
func HeapSrort[T cmp.Ordered](arg []T) []T {
if len(arg) <= 0 {
return nil
}
heap := NewMinHeap[T]()
heap.Heapify(arg)
sorted := make([]T, 0, len(arg))
for range heap.Array {
val, ok := heap.PopMin()
if !ok {
break
}
sorted = append(sorted, val)
}
return sorted
}
// PriorityQueue
type PriorityQueue[T cmp.Ordered] struct {
heap MinHeap[T]