# 堆排序及其应用

1、删除最大元素
2、插入新的元素

public class MaxPQ <Key extends Comparable<Key>> {
private Key[] pq;
private int N=0;
//0位置不放置元素
MaxPQ(int max){
pq=(Key[]) new Comparable[max+1];
}
void insert(Key v){
//调整数组大小 为原先的2倍
if(N+1==pq.length){
resize(2*pq.length);
}
pq[++N]=v;
swim(N);
}

Key max(){
return pq[1];
}

Key delMax(){
Key max=pq[1];
swap(1,N--);
sink(1);
pq[N+1]=null;
if(N>0 && N+1==pq.length/4){
resize(pq.length/2);
}
return max;
}

boolean isEmpty(){
return N==0;
}

int size(){
return N;
}

private void swim(int k){
while(k>1 && less(k/2,k)){
swap(k/2,k);
k=k/2;
}
}
private void sink(int k){
while(2*k<=N){
int j=2*k;
if(j+1<=N && less(j,j+1)){
j++;
}
if(less(k,j)){
swap(k,j);
//向下移动
k=j;
}else {
break;
}
}
}
private void swap(int i,int j){
Key a=pq[i];
pq[i]=pq[j];
pq[j]=a;
}

private boolean less(int i,int j){
return pq[i].compareTo(pq[j])<0;
}
private void resize(int max){
Key[] temp=(Key[]) new Comparable[max];
for(int i=1;i<=N;i++){
temp[i]=pq[i];
}
pq=temp;
}
public static void main(String[] args) {
MaxPQ pq=new MaxPQ(4);
pq.insert("P");
pq.insert("Q");
pq.insert("E");
String s=(String)pq.delMax();
System.out.println(s);
pq.insert("X");
pq.insert("A");
pq.insert("M");
System.out.println(pq.delMax());
pq.insert("P");
pq.insert("L");
pq.insert("E");
System.out.println(pq.delMax());
System.out.println(pq.max());
}
}


public class HeapSort {
public void heapSort(Comparable[] a){
int index=a.length-1;
//构建有序堆
for(int i=(index-1)/2;i>=0;i--){
sink(a,i,index);
}
for(int i=index;i>=0;i--){
swap(a,0,index--);
sink(a,0,index);
}
}

//N是最后一个元素的位置
public void sink(Comparable[] a,int k,int N){
while(2*k+1<=N){
int j=2*k+1;
if(j+1<=N && less(a,j,j+1)){
j++;
}
if(less(a,k,j)){
swap(a,k,j);
k=j;
}else{
break;
}

}
}

private boolean less(Comparable[] a,int i,int j){
return a[i].compareTo(a[j])<0;
}

private void swap(Comparable[] a,int i,int j){
Comparable temp=a[i];
a[i]=a[j];
a[j]=temp;
}

public static void main(String[] args) {
Integer[] a=new Integer[]{0,2,1,4,3,7,9,6,5,0};
HeapSort s=new HeapSort();
s.heapSort(a);
for(int i=0;i<a.length;i++){
System.out.print(a[i]+" ");
}
}

}


02-19 496
09-12 255

02-25 206
04-05 556
05-29 1262
09-30 1237
10-03 8505
06-06 1909
11-07 2013
06-12 2639
10-23 52
05-19 1024
03-17
05-23 195
10-14 2821
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客