堆排序的过程:
![](https://images2015.cnblogs.com/blog/895536/201603/895536-20160318132404490-2043248502.png)
![](https://images2015.cnblogs.com/blog/895536/201603/895536-20160318132410224-97336863.png)
![](https://images2015.cnblogs.com/blog/895536/201603/895536-20160318132416178-1365874358.png)
![](https://images2015.cnblogs.com/blog/895536/201603/895536-20160318132421006-1948155848.png)
![](https://images2015.cnblogs.com/blog/895536/201603/895536-20160318132426490-1842857322.png)
![](https://images2015.cnblogs.com/blog/895536/201603/895536-20160318132431662-1517739033.png)
![](https://images2015.cnblogs.com/blog/895536/201603/895536-20160318132437490-2143652067.png)
![](https://images2015.cnblogs.com/blog/895536/201603/895536-20160318132442428-6449493.png)
![](https://images2015.cnblogs.com/blog/895536/201603/895536-20160318132447459-172621453.png)
![](https://images2015.cnblogs.com/blog/895536/201603/895536-20160318132451928-540224526.png)
![](https://images2015.cnblogs.com/blog/895536/201603/895536-20160318132457240-374349078.png)
/**
* Created by root on 16-3-12.
*/
public class BinaryHeap<E extends Comparable> {
//internal class
public static class ArrayList<T> {
public static int DEFAULT_SIZE = 8;
T[] data;
int size;
//constructor
public ArrayList() {
data = (T[]) new Object[DEFAULT_SIZE];
size = 0;
}
public ArrayList(int size) {
data = (T[]) new Object[size];
size = 0;
}
//isEmpty:
public boolean isEmpty() {
return size == 0;
}
//add:
public void add(T x){
this.set(size,x);
size++;
}
//get
public T get(int index) {
if (index > size || index < 0) {
throw new IndexOutOfBoundsException();
}
return data[index];
}
//set
public void set(int index, T target) {
if (index > data.length || index < 0) {
throw new IndexOutOfBoundsException();
}
data[index] = target;
}
}
//fields
ArrayList array;
public boolean isEmpty() {
return array.size == 0;
}
public boolean isFull() {
return array.size == array.data.length;
}
//constructor
public BinaryHeap() {
array = new ArrayList();
}
public void insert(E target) {
if(isFull()){
enlargeArray();
}
int hole=array.size;
while(hole>0){
int comparison=target.compareTo(array.get(getParent(hole)));
if(comparison<0){
array.set(hole,array.get(getParent(hole)));
hole=getParent(hole);
}
else {
break;
}
}
array.set(hole,target);
array.size++;
}
public Object findMin(){
return array.get(0);
}
//remove min
public E removeMin(){
E minItem=(E)findMin();
E lastItem=(E)array.get(array.size-1);
array.size--;
int hole=0;
for(int child=getChild(hole);child<array.size;child=getChild(hole)){
array.set(hole,array.get(child));
hole=child;
}
array.set(hole,lastItem);
return minItem;
}
public int getChild(int parent){
int leftChildIndex=parent*2+1;
int rightChildIndex=parent*2+2;
if(rightChildIndex>array.size-1){
return leftChildIndex;
}
E leftChildItem=(E)array.get(leftChildIndex);
E rightChildItem=(E)array.get(rightChildIndex);
int comparison=leftChildItem.compareTo(rightChildItem);
if(comparison<0){
return leftChildIndex;
}
return rightChildIndex;
}
public int getParent(int child){
return (child-1)/2;
}
//enlarge
public void enlargeArray(){
ArrayList oldArray=array;
ArrayList newArray=new ArrayList(array.data.length*2+1);
for(int i=0;i<oldArray.size;i++){
newArray.set(i,oldArray.get(i));
}
this.array=newArray;
array.size=oldArray.size;
}
}