如果使用向上调整,我们在建堆的时候遍历数组要从后到前遍历。
int[] array1 = new int[] {9,5,2,7,3,6,8};
for (int i = array1.length - 1; i >= 0; i --) {
shiftUp(array1, array1.length ,i);
}
小堆的操作和实现:
//堆的向下调整
public static void shiftDown(int[] array, int size, int index) {
int patent = index;
int child = 2 * patent + 1;
while (child < size) {
if(child + 1 < size && array[child + 1] < array[child]) {//取俩个结点的最小值
child += 1;
}
if(array[child] < array[patent]) {//孩子的值小于parent的值
int temp = array[child];
array[child] = array[patent];
array[patent] = temp;
}else {
break;
}
patent = child;
child = 2 * patent + 1;
}
}
//向上调整
public static void shiftUp(int[] array, int size, int index) {
int child = index;
int parent = (child - 1)/2;
while (child > 0) {
if(array[child] < array[parent]) {//孩子的值小于parent的值
int temp = array[child];
array[child] = array[parent];
array[parent] = temp;
}else {
break;
}
child = parent;
parent = (child - 1)/2;
}
}
建大堆是每次循环找大的值,而建小堆就是每次循环找小的值。
同理建小堆的时候用向下调整还是会出现失败反例(最小值出现在后几个数字),而使用向上调整是不会出现这种情况的。
**建小堆的时候用向下调整还是从前到后遍历数组。(因为index是根结点)
使用向上调整建立小堆的时候还是从后到前遍历数组。(因为index是最后一个叶子结点)**
优先级队列的实现:
优先级队列本质还是队列只是我们在实现offer和poll操作的时候分别进行了向上调整和向下调整。(当然我们使用建大堆的向上和向下调整的话是值最高的优先级最高,最先出队列)
public class MyPriorQueue {
private int[] array = new int[100];
private int size = 0;
public void offer(int data) {
this.array[size] = data;
size ++;
shiftUp(array, size, size - 1);
}
private void shiftUp(int[] array, int size, int index) {
int child = index;
int parent = (child - 1)/2;
while (child > 0) {
if(array[child] > array[parent]) {
int temp = array[child];
array[child] = array[parent];
array[parent] = temp;
}else {
break;
}
child = parent;
parent = (child - 1)/2;
}
}
public Integer poll() {
if(size <= 0) {
return null;
}
int ret = this.array[0];
this.array[0] = this.array[size - 1];
size --;
shiftDown(array, size, 0);
return ret;
}
private void shiftDown(int[] array, int size, int index) {
int parent = index;
int child = 2 * parent + 1;
while (child < size) {
if(child + 1 < size && array[child] < array[child + 1]) {
child += 1;
}
if(array[child] > array[parent]) {
int temp = array[child];
array[child] = array[parent];
array[parent] = temp;
}else {
break;
}
parent = child;
child = 2 * parent + 1;
}
}
public Integer peek() {
if(size <= 0) {
return null;
}
return array[0];
}
public boolean empty() {
return size == 0;
}
/* public void display() {
for (int i:this.array
) {
System.out.print(i + " ");
}
System.out.println();
}*/
public int length() {
return size;
}
}
**时间复杂度分析:
入队列-》向上调整O(logN)
出队列-》向下调整O(logN)
取队首元素O(1)
建堆操作的时间复杂度是O(N)**
**PriorityQueue 的使用:
(也就是库优先级队列)**
public static void main(String[] args) {
PriorityQueue pq = new PriorityQueue<>();
int[] nums = new int[] {3, 7, 4, 8, 3, 2, 9};
for (int i:nums
) {
pq.offer(i);
}
while (!pq.isEmpty()) {
System.out.print(pq.poll() + " ");
}
}
**我们要注意库函数里的优先级队列使用的是建小堆的向上调整和向下调整。
库里的原始优先级队列遵循值越小,优先级越高思想。
当然我们也可以自己定义一个比较器对象,借助比较器我们自己定义谁优先谁不优先。**
static class comp implements Comparator {
@Override
public int compare(Integer o1, Integer o2) {
// return o1 - o2;
return o2 - o1;
}
}
public static void main(String[] args) {
PriorityQueue pq = new PriorityQueue<>(new comp());
int[] nums = new int[] {3, 7, 4, 8, 3, 2, 9};
for (int i:nums
) {
pq.offer(i);
}
while (!pq.isEmpty()) {
System.out.print(pq.poll() + " ");
}
}
TopK 问题、堆排序:
**只要掌握了优先级队列,这两问题是很简单的。
我们知道如果我们使用建大堆的调整思想,我们每次出队列的一定是当前队列里优先级最高的,也就是值最大的。**
所以TopK我们只要出K次队列即可。
堆排序就是挨个出队列,就是这么简单。
升序:
public class Test {
static class comp implements Comparator {
@Override
public int compare(Integer o1, Integer o2) {
// return o1 - o2;
return o2 - o1;
}
}
public static void main(String[] args) {
PriorityQueue pq = new PriorityQueue<>(new comp());
int[] nums = new int[]{3, 7, 4, 8, 3, 2, 9};
for (int i : nums
) {
pq.offer(i);
}
while (!pq.isEmpty()) {
System.out.print(pq.poll() + " ");
}
}
}
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。**
[外链图片转存中…(img-6ff3cGS5-1715363563618)]
[外链图片转存中…(img-9pmznPUU-1715363563619)]
[外链图片转存中…(img-oGrho4Ut-1715363563619)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!