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


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

  • 相关阅读:
    ContactManager示例解析
    CubeLiveWallpaper例子解析
    BluetoothChat例子解析
    推荐一个模板引擎 templateengine
    jQuery plugin: Autocomplete
    乐从网站建设、域名、主机-www.lecong.me-www.lecong.mobi
    C#操作注册表
    .NET模板引擎
    [转]模版引擎AderTemplate源代码分析笔记
    windows服务器文件同步,网站同步镜像
  • 原文地址:https://www.cnblogs.com/orange0/p/15566536.html
Copyright © 2020-2023  润新知