当前位置:网站首页>Heap sorting and hardening heap code for memory
Heap sorting and hardening heap code for memory
2022-06-10 20:53:00 【sword to coding】
Heap sort
public static void getBigHeap(int[] arr){
for(int i=0;i<arr.length;i++){
heapInsert(arr,i);
}
}
/** * Get a random array * @param maxLength * @param maxValue * @return */
public static int[] getRandomArr(int maxLength,int maxValue){
int len=(int)(Math.random()*maxLength)+2;
int[] arr=new int[len];
for(int i=0;i<len;i++){
arr[i]=(int)(Math.random()*maxValue);
}
return arr;
}
/** * Pass in a large root heap array , The last one index It does not enter the big root heap * Our goal is to The newly added data is placed at the last of the array , Then gradually compare it with its parent node * Finally, the large root heap is satisfied * @param arr * @param index */
public static void heapInsert(int[] arr ,int index){
while(arr[(index-1)/2]<arr[index]){
swap(arr,(index-1)/2,index);
index=(index-1)/2;
}
}
/** * Check whether the subsequent nodes of a node conform to the large root heap , If it does not meet the requirements, it shall be adjusted * Adjust downward * @param arr * @param heapSize */
public static void heapIfy(int[] arr,int index,int heapSize){
int left= index*2+1;
while(left<heapSize){
int largest=left+1<heapSize&&arr[left+1]>arr[left]?left+1:left;
largest=arr[largest]>arr[index]?largest:index;
if(index==largest){
break;
}
swap(arr,largest,index);
index=largest;
left=index*2+1;
}
}
public static void swap(int[] arr,int a,int b){
int temp=arr[a];
arr[a]=arr[b];
arr[b]=temp;
}
Reinforced reactor
/** * Reinforced reactor . Added reverse index table * * Heap */
public class HeapGreater<T> {
// An array to hold the heap
private ArrayList<T> heap;
// Reverse index table
private HashMap<T,Integer> indexMap;
// Heap size
private int heapSize;
// The comparator
private Comparator<? super T> comp;
// Initialize the enhanced heap
public HeapGreater(Comparator<T> c){
this.comp=c;
indexMap=new HashMap<>();
heapSize=0;
heap=new ArrayList<>();
}
public boolean isEmpty(){
return this.heapSize==0;
}
public int size(){
return this.heapSize;
}
public boolean contains(T obj){
return indexMap.containsKey(obj);
}
public T peek(){
return heap.get(0);
}
public void push(T obj){
heap.add(obj);
indexMap.put(obj,heapSize);
heapInsert(heapSize++);
}
public T pop(){
T ans=heap.get(0);
swap(0,heapSize-1); // Move the head to the tail
indexMap.remove(ans);
heap.remove(--heapSize);
heapify(0); // Reposition the heap from the beginning
return ans;
}
// Remove any node on the heap
public void remove(T obj){
T replace=heap.get(heapSize-1);
int index=indexMap.get(obj);
indexMap.remove(obj);
heap.remove(--heapSize);
if(obj!=replace){
heap.set(index,replace);
indexMap.put(replace,index);
resign(replace);
}
}
/** * Perform heap adjustment on the specified heap node * @param obj */
public void resign(T obj){
heapInsert(indexMap.get(obj));
heapify(indexMap.get(obj));
}
/** * Return all elements * @return */
public List<T> returnAllElements(){
ArrayList<T> ts = new ArrayList<>();
for(T t: heap){
ts.add(t);
}
return ts;
}
/** * The last element in the heap is added to the heap * @param index */
public void heapInsert(int index){
while(comp.compare(heap.get(index),heap.get((index-1)/2))<0){
swap(index,(index-1)/2);
index=(index-1)/2;
}
}
/** * Adjust the element at the specified position , Heap it * @param index */
public void heapify(int index){
int left=index*2+1;
while(left<heapSize){
int best=left+1<heapSize&&comp.compare(heap.get(left+1),heap.get(left))<0?(left+1):left;
best=comp.compare(heap.get(best),heap.get(index))<0?best:index;
if(best==index){
break;
}
swap(best,index);
index=best;
left=index*2+1;
}
}
/** * In exchange for * @param i * @param j */
public void swap(int i,int j){
T o1=heap.get(i);
T o2=heap.get(j);
heap.set(i,o2);
heap.set(j,o1);
indexMap.put(o2,i);
indexMap.put(o1,j);
}
}
The most important of the two heaps is 3 A way heapify,heapInsert ,swap
边栏推荐
- When can Flink support the SQL client mode? When can I specify the applicati for submitting tasks to yarn
- An old programmer of about 10 years said: simple crud function enters the era of codeless development 1. Adding, deleting, modifying and checking interface information
- What are the conditions for opening an account for agricultural futures? How much is the service charge for opening an account now?
- Explain L3 cache to solve circular dependency
- 获取列表中最大最小值的前n个数值的位置索引的四种方法
- 割舍绳子/整数分割
- 移动端渲染原理浅析
- Key points of lldp protocol preparation
- The most common habits from more than 200 English papers written by gradua
- LeetCode:497. 非重叠矩形中的随机点————中等
猜你喜欢

详解三级缓存解决循环依赖

中国工科研究生200多篇英文论文中最常见的习惯(The Most Common Habits from more than 200 English Papers written by Gradua)

hidden danger? Limited meaning? Can't stop the real cooking Mini kitchenware hot 618

canvas 高级功能(中)

連接mysql報錯 errorCode 1129, state HY000, Host ‘xxx‘ is blocked because of many connection errors

Kcon 2022 topic public selection is hot! Don't miss the topic of "favorite"

京东发布基于张量网络加速的大规模、分布式量子机器学习平台TeD-Q

Microsoft Word tutorial, how to change page orientation and add borders to pages in word?

8.4v dual lithium battery professional charging IC (fs4062a)

Canvas advanced functions (Part 1)
随机推荐
RuntimeError: Attempting to deserialize object on CUDA device 1 but torch. cuda. device_ count() is 1.
Connexion MySQL errorcode 1129, State hy000, Host 'xxx' is Blocked because of many Connection Errors
Vertical bar of latex tips absolute value \left\right|
Why do some web page style attributes need a colon ":" and some do not?
redis设置密码命令(临时密码)
轻便型FDW框架 for pb
Game compatibility test (general scheme)
MySQL ---- 常用函数
node(express)实现增删改查、分页等接口
table设置超出部分隐藏,鼠标移上去显示全部
电子招标采购商城系统:优化传统采购业务,提速企业数字化升级
[enter textbook latex record]
Uni app custom navigation
Is Jiuzhou futures regular? Is it safe to open an account
P5723 [deep base 4. example 13] prime number pocket
解决idea超过5个相同包的时候自动变成*的问题
Mysql database foundation
hidden danger? Limited meaning? Can't stop the real cooking Mini kitchenware hot 618
Fs4100 lithium battery charging management IC input 12V to 8.4v charging IC
Handwritten code bind