一文告别堆:堆的结构代码+堆排序

堆的概述加代码实现

大根堆和小根堆
在这里插入图片描述
普通数组堆化的代码(两种方法)

public static  void heapInsert(int[] heap,int index){//先插入到数组的最后一个位置,然后根据那个位置去调整堆[大根]
            while(heap[index]>heap[(index-1)/2]){//一直和父亲比较(到了根节点,还没有结束,根节点的父亲还是根节点)
                swap(heap,index,(index-1)/2);
                index=(index-1)/2;
            }
    }








    public static void heapify(int[] heap, int index, int heapSize){//往下比较交换
        int left=index*2+1;//抓到左孩子
        while(left<heapSize){
            //最大的结点,两个孩子争取,决胜者和父亲争取
            int largestIndex=left+1<heapSize&&heap[left+1]>heap[left]?left+1:left;
            largestIndex=heap[largestIndex]>heap[index]?largestIndex:index;
            if(index==largestIndex) break;//父亲是决胜者
            swap(heap,index,largestIndex);
            //小的元素来到大的元素原来的位置,(重复while循环,直到left越界,就是index无孩子了)
            index=largestIndex;
            left=index*2+1;
        }
    }
    public static void swap(int[] arr,int i,int j){
        int temp=arr[i];
        arr[i]=arr[j];
        arr[j]=temp;
    }

堆结构代码

private int[] heap;
    private int limit;
    private int heapSize;

    public MyHeap_code(){
        heap=new int[limit];
    }
    public boolean isEmpty(){
        return heapSize==0;
    }
    public boolean isFull(){
        return heapSize==limit;
    }

    public void push(int value){
        if (heapSize==limit) throw new RuntimeException("the heap is full");
        heap[heapSize]=value;
        heapInsert(heap,heapSize++);
    }
    public int pop(){
        if (heapSize==0) throw new RuntimeException("the heap is empty");
        int ans=heap[0];
        swap(heap,0,--heapSize);
        heapify(heap,0,heapSize);
        return ans;
    }

堆排序

在这里插入图片描述

public static void heapSort(int[] arr) {
       if(arr==null || arr.length<2) return;
       //建立大根堆
//        for (int i = 0; i < arr.length; i++) {
//            heapInsert(arr,i);
//        }
        for(int i=arr.length-1;i>=0;i--) {
            //(heapInsert往下越多的结点,越多的结点向上比较的极限是logn)=====logn的极限让多的结点对应
            //(heapify)让底下多的结点,调整的时候极限没有到底logn,只有上层的结点的极限才是有logn极限===logn的极限让少数结点对应
            heapify(arr, i, arr.length);//从底下向上(调整堆)...比较insert的效率大
        }
        int heapSize=arr.length;
        swap(arr,0,--heapSize);
        while(heapSize>0){//n-1次
            heapify(arr,0,heapSize);
            swap(arr,0,--heapSize);
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赑屃爱Java

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值