堆排序的算法实现---大顶堆

package heap;

public class maxheap {

    public static void main(String[] args){
        MaxHeapp mp=new MaxHeapp(5);
        mp.add(6);
        mp.add(5);
        mp.add(8);
        mp.toString();
        mp.add(9);
        mp.toString();
        System.out.print(mp.gets());
        System.out.print(mp.pop());
        System.out.print(mp.pop());
        System.out.print(mp.pop());
        System.out.print(mp.pop());
    }
    
}
package heap;

public class MaxHeapp{
    int [] maxHeap;
    int heapSize;
    int realSize=0;
    
    public MaxHeapp(int heapSize){
        this.heapSize=heapSize;
        maxHeap=new int[heapSize +1];
        maxHeap[0]=0;//
    }
    public void add(int element){
        realSize++;
        if(realSize>heapSize){
            System.out.print("too many element");
            realSize--;
            return;
        }
    
        maxHeap[realSize]=element;
        int index=realSize;
        int parent=index/2;
        while(maxHeap[index]>maxHeap[parent]&&index>1){
            int temp=maxHeap[index];
            maxHeap[index]=maxHeap[parent];
            maxHeap[parent]=temp;
            index=parent;
            parent=index/2;
        }
        
    }
    public int gets(){
        
            return maxHeap[1];
        
    }
        public int pop(){
            if(realSize<1){
                System.out.println("Erroy!!!");
                return 0;
            }
            else{
                int removeElement=maxHeap[1];
                maxHeap[1]=maxHeap[realSize];
                realSize--;
                int index=1;
                while(index<realSize&&index<realSize/2){
                    int left=index*2;
                    int right=(index*2)+1;
                    if(maxHeap[index]<maxHeap[left] || maxHeap[index]<maxHeap[right]){
                        if(maxHeap[index]<maxHeap[left]){
                            int temp=maxHeap[left];
                            maxHeap[left]=maxHeap[index];
                            maxHeap[index]=temp;
                            index=left;
                        }else{
                            int temp=maxHeap[right];
                            maxHeap[right]=maxHeap[index];
                            maxHeap[index]=temp;
                            index=right;
                        }
                    }else{
                        break;
                    }
                }
                return removeElement;
            }
        }
        
    public int size(){
        return realSize;
    }
    public String toString(){
        if(realSize==0){
            return "no element";
        }else{
            StringBuilder sb=new StringBuilder();
            sb.append('[');
            for(int i=1;i<realSize;i++){
                sb.append(maxHeap[i]);
                sb.append(',');
            }
            sb.deleteCharAt(sb.length()-1);
            sb.append(']');
            return sb.toString();
        }
    }
    
}

注意好子节点和父节点之间的关系,数组是从1开始而不是0,因为可以使用parent=child/2子句,而不用分类讨论

 

posted @ 2021-11-17 11:51  橘子味汽水  阅读(50)  评论(0编辑  收藏  举报