go语言的堆排序实现

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录


提示:以下是本篇文章正文内容,下面案例可供参考

一、intheap的堆排序接口

go 复制代码
// An IntHeap is a min-heap of ints.
type IntHeap []int

func (h IntHeap) Len() int           { return len(h) }
func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] }
func (h IntHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *IntHeap) Push(x any) {
	// Push and Pop use pointer receivers because they modify the slice's length,
	// not just its contents.
	*h = append(*h, x.(int))
}

func (h *IntHeap) Pop() any {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

二、节点的堆排序实现

go 复制代码
// An Item is something we manage in a priority queue.
type Item struct {
	value    string // The value of the item; arbitrary.
	priority int    // The priority of the item in the queue.
	// The index is needed by update and is maintained by the heap.Interface methods.
	index int // The index of the item in the heap.
}

// A PriorityQueue implements heap.Interface and holds Items.
type PriorityQueue []*Item

func (pq PriorityQueue) Len() int { return len(pq) }

func (pq PriorityQueue) Less(i, j int) bool {
	// We want Pop to give us the highest, not lowest, priority so we use greater than here.
	return pq[i].priority > pq[j].priority
}

func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
	pq[i].index = i
	pq[j].index = j
}

func (pq *PriorityQueue) Push(x any) {
	n := len(*pq)
	item := x.(*Item)
	item.index = n
	*pq = append(*pq, item)
}

func (pq *PriorityQueue) Pop() any {
	old := *pq
	n := len(old)
	item := old[n-1]
	old[n-1] = nil  // avoid memory leak
	item.index = -1 // for safety
	*pq = old[0 : n-1]
	return item
}

三、leetcode 23. 合并 K 个升序链表

23. 合并 K 个升序链表

go 复制代码
/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func mergeKLists(lists []*ListNode) *ListNode {
	if len(lists) == 0 {
		return nil
	}
	dummy := &ListNode{
		Val:  -1,
		Next: nil,
	}
	p := dummy
	pq := &PriorityQueue{}
	for _, h := range lists {
		if h != nil {
			heap.Push(pq, h)
		}
	}
	for pq.Len() != 0 {
		node := heap.Pop(pq).(*ListNode)
		p.Next = node
		if node.Next != nil {
			heap.Push(pq, node.Next)
		}
		p = p.Next
	}
	return dummy.Next

}

// A PriorityQueue implements heap.Interface and holds Items.
type PriorityQueue []*ListNode

func (pq PriorityQueue) Len() int { return len(pq) }

func (pq PriorityQueue) Less(i, j int) bool {
	// We want Pop to give us the highest, not lowest, priority so we use greater than here.
	return pq[i].Val < pq[j].Val
}

func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
}

func (pq *PriorityQueue) Push(x any) {
	*pq = append(*pq, x.(*ListNode))
}

func (pq *PriorityQueue) Pop() any {
	old := *pq
	n := len(old)
	item := old[n-1]
	old[n-1] = nil // avoid memory leak
	*pq = old[0 : n-1]
	return item
}

相关推荐
一只叫煤球的猫13 分钟前
你真的会用 return 吗?—— 11个值得借鉴的 return 写法
java·后端·代码规范
Asthenia041225 分钟前
HTTP调用超时与重试问题分析
后端
颇有几分姿色40 分钟前
Spring Boot 读取配置文件的几种方式
java·spring boot·后端
AntBlack42 分钟前
别说了别说了 ,Trae 已经在不停优化迭代了
前端·人工智能·后端
曹牧1 小时前
Java 调用webservice接口输出xml自动转义
java·开发语言·javascript
@淡 定1 小时前
Spring Boot 的配置加载顺序
java·spring boot·后端
Asthenia04121 小时前
Java线程池线程工厂深入剖析:从生产需求到面试拷问
后端
pyengine2 小时前
基于pandoc的MarkDown格式与word相互转换小工具开发(pyqt5)
开发语言·python·qt·word
YuSun_WK2 小时前
配置MambaIRv2: Attentive State Space Restoration的环境
开发语言·python