当前位置:网站首页>手写大根堆
手写大根堆
2022-08-09 12:01:00 【爱敲代码的Harrison】
用数组实现一个大根堆
实现功能
- 每给一个数加进数组后,自动调整为大根堆结构(heapInsert()方法)
- 返回大根堆中的最大值,并且在大根堆中把最大值删除,且剩下的数,依然保持为大根堆结构(heapify()方法)
自己手写一个大根堆结构 VS 纯暴力的方法,用对数器测试100万组!!!
package com.harrison.class04;
public class Code01_MaxHeap {
public static class MyMaxHeap {
private int[] heap;
private final int limit;
private int heapSize;
public MyMaxHeap(int limit) {
heap = new int[limit];
heapSize = 0;
this.limit = limit;
}
public void push(int value) {
if (heapSize == limit) {
throw new RuntimeException("heap is full");
}
heap[heapSize] = value;
heapInsert(heap, heapSize++);
}
// 堆结构的两个关键操作:从某个位置开始往上看
// 动态地建立大根堆
// 如果收了N个数,时间复杂度为logN
public static void heapInsert(int[] arr, int index) {
while (arr[index] > arr[(index - 1) / 2]) {
swap(arr, index, (index - 1) / 2);
index = (index - 1) / 2;
}
}
// 返回最大值,并在大根堆中把最大值删掉
// 剩下的数,依然保持大根堆组织
public int pop() {
int ans = heap[0];
swap(heap, 0, --heapSize);
heapify(heap, 0, heapSize);
return ans;
}
// 堆结构的两个关键操作:从某个位置开始往下调,时间复杂度logN
// heapSize 自己想象范围的大小,并不是数组的实际大小!!!
public static void heapify(int[] arr, int index, int heapSize) {
int left=(2*index)+1;
while(left<heapSize) {
int largest=(left+1<heapSize && arr[left+1]>arr[left])?left+1:left;
int allLargest=arr[largest]>arr[index]?largest:index;
if(allLargest==index) {
break;
}
swap(arr, allLargest, index);
index=allLargest;
left=(2*index)+1;
}
}
public boolean isEmpty() {
return heapSize==0;
}
public boolean isFull() {
return heapSize==limit;
}
}
public static void swap(int[] arr, int i, int j) {
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
public static class RightMaxHeap{
private int[] arr;
private int size;
private final int limit;
public RightMaxHeap(int limit) {
this.limit=limit;
arr=new int[limit];
size=0;
}
public void push(int value) {
if(size==limit) {
throw new RuntimeException("heap is full");
}
arr[size++]=value;
}
public int pop() {
int maxIndex=0;
for(int i=1; i<size; i++) {
if(arr[maxIndex]<arr[i]) {
maxIndex=i;
}
}
int ans=arr[maxIndex];
arr[maxIndex]=arr[--size];
return ans;
}
public boolean isEmpty() {
return size==0;
}
public boolean isFull() {
return size==limit;
}
}
public static void main(String[] args) {
int testTimes=1000000;
int limit=1000;
int value=100;
System.out.println("test begin");
for(int i=0; i<testTimes; i++) {
// [1,1000],防止出现零,否则一开始堆的大小就为0
int curLimit=(int)(Math.random()*limit)+1;
MyMaxHeap my=new MyMaxHeap(curLimit);
RightMaxHeap test=new RightMaxHeap(curLimit);
int curOopsTimes=(int)(Math.random()*limit);
for(int j=0; j<curOopsTimes; j++) {
if(my.isEmpty()!=test.isEmpty()) {
System.out.println("Oops");
}
if(my.isFull()!=test.isFull()) {
System.out.println("Oops");
}
if(my.isEmpty()) {
int curValue=(int)(Math.random()*(value+1));
my.push(curValue);
test.push(curValue);
}else if(my.isFull()) {
if(my.pop()!=test.pop()) {
System.out.println("Oops");
}
}else {
if(Math.random()<0.5) {
int curValue=(int)(Math.random()*(value+1));
my.push(curValue);
test.push(curValue);
}else {
if(my.pop()!=test.pop()) {
System.out.println("Oops");
}
}
}
}
}
System.out.println("finish");
}
}
边栏推荐
- 网页控制台控制编辑框
- Golang学习之路(五):Golang的函数
- GPT-3组合DALL·E,60秒内搞定游戏设定和原型动画!网友看后:这游戏想玩
- 微服务架构的核心关键点
- Byte Qiu Zhao confused me on both sides, and asked me under what circumstances would the SYN message be discarded?
- Apexsqlrecover cannot connect to database
- 放下手机吧:实验表明花20分钟思考和上网冲浪同样快乐
- proto3-2 syntax
- 从零开始Blazor Server(9)--修改Layout
- 一甲子,正青春,CCF创建六十周年庆典在苏州举行
猜你喜欢
Too much volume... Tencent was asked on the side that the memory was full, what would happen?
How should the acceptance criteria for R&D requirements be written?| Agile Practices
脱光衣服待着就能减肥,当真有这好事?
Reading and writing after separation, performance were up 100%
水能自发变成“消毒水”,83岁斯坦福教授:揭示冬天容易得流感的部分原因...
API调用,API传参,面向对接开发,你真的会写接口文档吗?
Manchester city launch emotional intelligence scarf can be detected, give the fans
【Untitled】
无需精子卵子子宫体外培育胚胎,Cell论文作者这番话让网友们炸了
放下手机吧:实验表明花20分钟思考和上网冲浪同样快乐
随机推荐
The core key points of microservice architecture
无需精子卵子子宫体外培育胚胎,Cell论文作者这番话让网友们炸了
京东架构师呕心整理:jvm与性能调优有哪些核心技术知识点
放下手机吧:实验表明花20分钟思考和上网冲浪同样快乐
How to upload local file trial version in binary mode in ABAP report
#物联网征文#小熊派设备开发实战
Blazor Server (9) from scratch -- modify Layout
金融业“限薪令”出台/ 软银出售过半阿里持仓/ DeepMind新实验室成立... 今日更多新鲜事在此...
基于STM32+铂电阻设计的测温仪
win10编译x264库(也有生成好的lib文件)
虚拟机安装出现的问题汇总
腾讯欲成育碧最大股东/ 米哈游招NLP内容生成研究员/ AI发现四千余物种濒临灭绝...今日更多新鲜事在此...
ACM longest non-descent subsequence problem
LeetCode #101. Symmetric Binary Tree
实验记录:搭建网络过程
The grep command Shell regular expressions, the three musketeers
阿里云新增三大高性能计算解决方案,助力生命科学行业快速发展
Summary of learning stages (knapsack problem)
报告:想学AI的学生数量已涨200%,老师都不够用了
阻塞、非阻塞、多路复用、同步、异步、BIO、NIO、AIO 一锅端