堆排序(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
    评论
堆排序是一种基于二叉堆数据结构的排序算法,它的时间复杂度为O(nlogn)。下面是一个使用Java实现的堆排序代码示例: ```java public class HeapSort { public static void heapSort(int[] arr) { int n = arr.length; // 构建最大堆 for (int i = n / 2 - 1; i >= 0; i--) heapify(arr, n, i); // 逐个将堆顶元素移到数组末尾 for (int i = n - 1; i > 0; i--) { // 将当前堆顶元素(最大值)与数组末尾元素交换 int temp = arr[0]; arr[0] = arr[i]; arr[i] = temp; // 重新构建最大堆 heapify(arr, i, 0); } } // 调整以index为根节点的子树为最大堆 public static void heapify(int[] arr, int n, int index) { int largest = index; // 初始化最大值为根节点 int left = 2 * index + 1; // 左子节点索引 int right = 2 * index + 2; // 右子节点索引 // 如果左子节点大于根节点,则更新最大值索引 if (left < n && arr[left] > arr[largest]) largest = left; // 如果右子节点大于当前最大值,则更新最大值索引 if (right < n && arr[right] > arr[largest]) largest = right; // 如果最大值不是根节点,则交换根节点和最大值 if (largest != index) { int swap = arr[index]; arr[index] = arr[largest]; arr[largest] = swap; // 递归调整交换后的子树 heapify(arr, n, largest); } } public static void main(String[] args) { int[] arr = { 12, 11, 13, 5, 6, 7 }; heapSort(arr); System.out.println("排序后的数组:"); for (int i : arr) { System.out.print(i + " "); } } } ```
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Wmx-98

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

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

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

打赏作者

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

抵扣说明:

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

余额充值