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


    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子句,而不用分类讨论

  • 相关阅读:
    2018-10-20-WPF-通过位处理合并图片
    2019-2-11-WPF-获取应用的所有窗口
    2019-2-11-WPF-获取应用的所有窗口
    2018-8-10-WPF-如何在绑定失败异常
    2018-8-10-WPF-如何在绑定失败异常
    类和对象
    类和对象
    什么是可串行化MVCC
    LeetCode-环形链表|+环形链表||
    用js仿探探拖拽卡片的效果、飞卡片的效果,感觉挺酷,最后有美女看哦!
  • 原文地址:https://www.cnblogs.com/orange0/p/15566536.html
Copyright © 2020-2023  润新知