MCPcopy Index your code
hub / github.com/TheAlgorithms/Go / HeapSort

Function HeapSort

sort/heapsort.go:122–143  ·  view source on GitHub ↗
(slice []T)

Source from the content-addressed store, hash-verified

120}
121
122func HeapSort[T constraints.Ordered](slice []T) []T {
123 N := len(slice)
124
125 moreFunc := func(i, j int) bool {
126 return slice[i] > slice[j]
127 }
128 swapFunc := func(i, j int) {
129 slice[i], slice[j] = slice[j], slice[i]
130 }
131
132 // build a maxheap
133 for i := N/2 - 1; i >= 0; i-- {
134 heapifyDown(slice, N, i, moreFunc, swapFunc)
135 }
136
137 for i := N - 1; i > 0; i-- {
138 slice[i], slice[0] = slice[0], slice[i]
139 heapifyDown(slice, i, 0, moreFunc, swapFunc)
140 }
141
142 return slice
143}

Callers

nothing calls this directly

Calls 1

heapifyDownFunction · 0.85

Tested by

no test coverage detected