Sorting algorithms/Heapsort: Difference between revisions

Go solution
m (→‎Icon and Unicon: header simplification)
(Go solution)
Line 755:
 
end program Heapsort_Demo</lang>
=={{header|Go}}==
<lang go>package main
 
import "fmt"
 
var a = []int{170, 45, 75, -90, -802, 24, 2, 66}
 
func main() {
fmt.Println("before:", a)
heapSort()
fmt.Println("after: ", a)
}
 
func heapSort() {
for start := (len(a) - 2) / 2; start >= 0; start-- {
siftDown(start, len(a)-1)
}
for end := len(a) - 1; end > 0; end-- {
a[end], a[0] = a[0], a[end]
siftDown(0, end-1)
}
}
 
 
func siftDown(start, end int) {
for root := start; root*2+1 <= end; {
child := root*2 + 1
if child+1 <= end && a[child] < a[child+1] {
child++
}
if a[root] >= a[child] {
return
}
a[root], a[child] = a[child], a[root]
root = child
}
}</lang>
 
=={{header|Haskell}}==
1,707

edits