golang 堆排序实现,暂时只实现数据写入堆
https://gitee.com/sinzuo/myTools 下
root@golang:~/k8s_golang/k8s_check/other_check# cat duipaixu.go
package main
import (
"fmt"
)
var data = [...]int{1,100,7,6,5,15,59,2,9,13}
func insertDui(pd **Node,start int,end int){
if start > end {
return
}
node := &Node{}
node.data = data[(start+end)/2]
*pd = node
insertDui(&(node.left),start ,(start+end)/2 -1)
insertDui(&(node.right),(start+end)/2 + 1,end)
}
type Node struct{
left *Node
data int
right *Node
}
func show(p *Node){
if p!=nil {
fmt.Println(p.data)
if p.left != nil {
show(p.left)
}
if p.right != nil {
show(p.right)
}
}
}
var p *Node
func main(){
insertDui(&p,0,len(data)-1)
show(p)
fmt.Println(data)
}
执行结果
5
100
1
7
6
2
15
59
9
13
[1 100 7 6 5 15 59 2 9 13]