小根堆 JAVA实现,真正的堆排序是怎么样炼成的

package lee.tools;

public class MinHeap {
	public int[] arr;
	int size;
	public int end;
	
	public MinHeap(){
		size = 5;
		end = -1;
		arr = new int[size];
	}
	public boolean isFull(){
		if(size-1==end){
			return true;
		}
		else{
			return false;
		}
	}
	public void enLarge(){
		size = size*2;
		int[] newArr = new int[size];
		for(int i=0;i<arr.length;i++){
			newArr[i] = arr[i];
		}
		arr = newArr;
	}
	public boolean isEmpty(){
		if(end==-1){
			return true;
		}
		else{
			return false;
		}
	}
	public void siftUp(int n){
		for(int i=n;i>0; i=(i-1)/2){
			int father = (i-1)/2;
			if(arr[i] < arr[father]){
				int temp = arr[i];
				arr[i] = arr[father];
				arr[father] = temp;
			}
			else{
				return;
			}
		}
	}
	public void siftDown(int n){
		for(int i = n*2+1;i<=end;i=i*2+1){
			if(arr[i+1]<arr[i]){
				i++;
			}
			int father = (i-1)/2;
			if(arr[i] < arr[father]){
				int temp = arr[i];
				arr[i] = arr[father];
				arr[father] = temp;
			}
			else{
				return;
			}
		}
		
	}
	public void insert(int data){
		if(isFull()){
			enLarge();
		}
		arr[++end] = data;
		siftUp(end);
	}
	public int outTop(){
		if(isEmpty()){
			System.out.println("The heap is empty!!");
			return -9999;
		}
		int temp = arr[0];
		arr[0] = arr[end--];
		siftDown(0);
		return temp;
	}
}




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值