堆排序-HeapSort

堆排序与快速排序,归并排序一样都是时间复杂度为O(N*logN)。
二叉堆的定义
- 二叉堆是完全二叉树或者是近似完全二叉树。
- 二叉堆满足二个特性:
- 1.父结点的键值总是大于或等于(小于或等于)任何一个子节点的键值。
- 2.每个结点的左子树和右子树都是一个二叉堆(都是最大堆或最小堆)。

当父结点的键值总是大于或等于任何一个子节点的键值时为最大堆。当父结点的键值总是小于或等于任何一个子节点的键值时为最小堆。

namespace HeapSort {
    class Solution {
    public:
        //在arr中0-length范围,以索引parent为根调整成最小堆
        //(前提parent左右子树都是最小堆)
        void adjustHeap(vector<int> &arr,int parent,int length){
            int tmp=arr[parent];    //先保存根parent的值
            int p=parent,c=2*p+1;   //设置p(parent)和c(child)
            while(c<length){
                //更新c为值小的c
                if(c+1<length && arr[c+1]<arr[c]){
                    c=c+1;
                }
                //如果tmp比c大,c值上移到p位置;同时更新p,c的值
                if(tmp>arr[c]){
                    arr[p]=arr[c];
                    p=c;
                    c=2*c+1;
                }else{  //否则,就是找到tmp的插入位置
                    break;
                }
            }
            //插入tmp到p位置
            arr[p]=tmp;
        }

        //
        void heapSort(vector<int> &arr){
            int length=arr.size();
            //从最后节点的父节点length/2开始到0,不断调整,达到建堆
            for(int i=length/2;i>=0;i--){
                adjustHeap(arr, i, length);
            }
            //每次把堆顶的值和尾部互换,然后尾部之前的新堆调整;如此循环
            for(int i=length-1;i>0;i--){
                swap(arr[0],arr[i]);
                adjustHeap(arr, 0, i);
            }
            //降序排列改到升序排列
            for(int i=0;i<length-i-1;i++){
                swap(arr[i],arr[length-i-1]);
            }
        }
    };

    class Test {
    public:
        static void main(){
            vector<int> vec{ 23, 65, 12, 3, 8, 76, 345, 90, 21, 75, 34, 61 };
            Solution s;
            s.heapSort(vec);
        }
    };
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
堆排序是一种常见的排序算法,具有稳定性高、效率高等优点。下面介绍一下使用Java实现的小根堆排序。 首先,我们需要定义一个小根堆类,用于存储待排序的数据。该类需要包含以下几个方法: 1. `Heap()`:构造函数,用于初始化小根堆。 2. `insert(int val)`:插入操作,将一个新的元素插入到小根堆中。 3. `deleteMin()`:删除操作,删除小根堆中的最小元素,并返回该元素的值。 4. `size()`:获取小根堆中元素的个数。 5. `isEmpty()`:判断小根堆是否为空。 接下来,我们就可以使用小根堆对待排序的数据进行排序了。具体的步骤如下: 1. 将待排序的数据存入小根堆中。 2. 依次从小根堆中删除最小元素,并将其存入数组中。 3. 最后,将数组反转,即可得到排序后的结果。 下面是具体的Java代码实现: ```java public class HeapSort { public static void heapSort(int[] arr) { Heap heap = new Heap(arr.length); for (int i = 0; i < arr.length; i++) { heap.insert(arr[i]); } for (int i = 0; i < arr.length; i++) { arr[i] = heap.deleteMin(); } // 反转数组 reverse(arr); } // 反转数组 private static void reverse(int[] arr) { int left = 0; int right = arr.length - 1; while (left < right) { int temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; left++; right--; } } // 小根堆类 static class Heap { private int[] heap; private int size; public Heap(int capacity) { heap = new int[capacity + 1]; size = 0; } public void insert(int val) { if (size == heap.length - 1) { throw new RuntimeException("Heap is full"); } int i = ++size; while (i != 1 && val < heap[i / 2]) { heap[i] = heap[i / 2]; i /= 2; } heap[i] = val; } public int deleteMin() { if (isEmpty()) { throw new RuntimeException("Heap is empty"); } int min = heap[1]; int last = heap[size--]; int i = 1; int child = 2; while (child <= size) { if (child < size && heap[child] > heap[child + 1]) { child++; } if (last > heap[child]) { heap[i] = heap[child]; i = child; child *= 2; } else { break; } } heap[i] = last; return min; } public int size() { return size; } public boolean isEmpty() { return size == 0; } } } ``` 使用该算法对数组进行排序: ```java int[] arr = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5}; HeapSort.heapSort(arr); System.out.println(Arrays.toString(arr)); // 输出 [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9] ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值