堆排序(JAVA)

堆排序(JAVA)


原理:

基本原理也是选择排序,只是不在使用遍历的方式查找无序区间的最大的数,而是通过堆来选择无序区间的最大的数。
注意: 排升序要建大堆;排降序要建小堆。

堆排序示意图


性能分析:

时间复杂度:O( n * log (n) )

空间复杂度度:O(1)

稳定性:不稳定


实现:

public static void heapSort(int[] arr){
        //1.先建立堆
        createHeap(arr);
        //2.需要循环的取出堆顶元素,和最后一个元素交换位置并删除之
        //  再从 0 位置进行调整
        int heapSize = arr.length;
        for(int i = 0; i < arr.length; i++){
            //交换 0 号元素和堆的最后一个元素
            swap(arr , 0 , heapSize - 1);
            //把最后一个元素从堆上删除
            heapSize--;
            //从 0 号位置开始往下调整
            shiftDown(arr,heapSize,0);
        }
    }
    public static void createHeap(int[] arr){
        for(int i = (arr.length - 1 - 1) / 2; i >= 0; i--){
            shiftDown(arr,arr.length,i);
        }
    }
    public static void shiftDown(int[] arr,int size, int index){
        int parent = index;
        int child = 2 * parent + 1;
        while(child < size){
            //先找出左右子树比较大的
            if(child + 1 < size && arr[child + 1] > arr[child]){
                child = child + 1;
            }
            //再去比较 child 和 parent
            if(arr[parent] < arr[child]){
                swap(arr, parent , child);
            }else{
                break;
            }
            parent = child;
            child = 2 * parent + 1;
        }
    }
    public static void swap(int[] arr ,int x,int y){
        int tmp = arr[x];
        arr[x] = arr[y];
        arr[y] = tmp;
    }

实现main函数:

public static void main(String[] args) {
        int[] arr = {1,3,5,7,2,9,6,3};
        heapSort(arr);
        System.out.println(Arrays.toString(arr));
    }
  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 12
    评论
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Wmx-98

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

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

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

打赏作者

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

抵扣说明:

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

余额充值