堆排序(Heapsort)-- 高级排序算法

1 堆排序(Heapsort)

堆排序(Heapsort)是指利用堆这种数据结构所设计的一种排序算法。
二叉堆本质上是一种完全二叉树,它分为两个类型:最大堆和最小堆。最大堆任何一个父节点的值,都大于等于它左右孩子节点的值。最小堆任何一个父节点的值,都小于等于它左右孩子节点的值。二叉堆的根节点叫做堆顶。最大堆和最小堆的特点,决定了在最大堆的堆顶是整个堆中的最大元素;最小堆的堆顶是整个堆中的最小元素。

算法描述

  • 将初始待排序关键字序列(R1,R2….Rn)构建成大顶堆,此堆为初始的无序区;
  • 将堆顶元素R[1]与最后一个元素R[n]交换,此时得到新的无序区(R1,R2,……Rn-1)和新的有序区(Rn),且满足R[1,2…n-1]<=R[n];
  • 由于交换后新的堆顶R[1]可能违反堆的性质,因此需要对当前无序区(R1,R2,……Rn-1)调整为新堆,- 然后再次将R[1]与无序区最后一个元素交换,得到新的无序区(R1,R2….Rn-2)和新的有序区(Rn-1,Rn)。不断重复此过程直到有序区的元素个数为n-1,则整个排序过程完成。

动图演示
在这里插入图片描述
代码实现

class Solution:
    def sortArray(self, nums: List[int]) -> List[int]:
        n = len(nums)
        if not nums or n==0: return []
        # 初始化建立大顶堆
        for i in range(n//2-1, -1, -1):
            self.maxHeapify(nums, n, i)
        # 取出堆顶元素
        for i in range(n-1, -1, -1):
            temp = nums[0]
            nums[0] = nums[i]
            nums[i] = temp
            self.maxHeapify(nums, i, 0)
        return nums

    def maxHeapify(self, nums, length, i):
        left, right = 2*i+1, 2*i+2
        largest = i
        if left < length and nums[left] > nums[largest]:
            largest = left
        if right < length and nums[right] > nums[largest]:
            largest = right
        if largest != i:
            temp = nums[i]
            nums[i] = nums[largest]
            nums[largest] = temp
            self.maxHeapify(nums, length, largest)

使用python内置的堆数据结构来排序:

import heapq
class Solution:
    def sortArray(self, nums: List[int]) -> List[int]:
        heap, res = [], []
        for i in nums:
            heapq.heappush(heap,i)
        while heap:
            res.append(heapq.heappop(heap))
        return res

算法特性

  • 时间复杂度(最好): O ( n l o g n ) O(nlogn) O(nlogn)
  • 时间复杂度(最坏): O ( n l o g n ) O(nlogn) O(nlogn)
  • 时间复杂度(平均): O ( n l o g n ) O(nlogn) O(nlogn)
  • 空间复杂度: O ( 1 ) O(1) O(1)
  • 稳定性:不稳定

参考资料

十大经典排序算法(动图演示)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
堆排序是一种常见的排序算法,具有稳定性高、效率高等优点。下面介绍一下使用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] ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值