Skip to content

Instantly share code, notes, and snippets.

@XDo0
Last active November 7, 2022 09:27
Show Gist options
  • Save XDo0/9ed3da8b6045346a085a30577caec357 to your computer and use it in GitHub Desktop.
Save XDo0/9ed3da8b6045346a085a30577caec357 to your computer and use it in GitHub Desktop.
Heap Sort based on PriorityQueue
import java.util.*;
/*Comparator接口说明:
* 返回负数,形参中第一个参数排在前面;返回正数,形参中第二个参数排在前面
* 对于队列:排在前面意味着往队头靠
* 对于堆(使用PriorityQueue实现):从队头到队尾按从小到大排就是最小堆(小顶堆),
* 从队头到队尾按从大到小排就是最大堆(大顶堆)--->队头元素相当于堆的根节点
* */
// 默认括号内的pair2>pair1,大的在队尾,升序
PriorityQueue<int[]> maxHeap = new PriorityQueue<>((pair1, pair2)->pair2[1]-pair1[1]);
PriorityQueue<int[]> minHeap = new PriorityQueue<>((pair1,pair2)->pair1[1]-pair2[1]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment