使用最小堆进行降序的堆排序

使用最小堆进行降序的堆排序

堆排序 – 堆排序是一种基于比较的算法,它使用二叉树数据结构按升序或降序对数字列表进行排序。它通过堆排序创建一个堆数据结构,其中根是最小元素,然后删除根,再次排序在根位置给出列表中第二小的数字。

最小堆 – 最小堆是一种数据结构,其中父节点始终小于子节点,因此根节点是所有元素中最小的元素。

问题陈述

给定一个整数数组。使用最小堆按降序对它们进行排序。

示例 1

Input: [2, 5, 1, 7, 0]
Output: [7, 5, 2, 1, 0]

示例 2

Input: [55, 1, 23, 10, 1]
Output: [55, 23, 10, 1, 1]

方法 1

为了使用最小堆按降序执行堆排序,我们创建一个元素的最小堆,并一次提取一个元素,通过反转顺序得到一个按降序排列的数组。

伪代码

procedure heapSort (arr[], n)   Initialize priority queue: minHeap   for i = 1 to n      add arr[i] to minHeap   i = n - 1   while minHeap is not empty      arr[i–] = top element of minHeap      Remove the top element of minHeapend procedure

示例:C++ 实现

在下面的程序中,我们使用最小堆对数组进行排序,然后反转顺序以获得结果。

#include using namespace std;// Function to heap sort in decreasing order using min heapvoid heapSort(int arr[], int n){   // Creating min heap using a priority queue   priority_queue<int, vector, greater > minHeap;      // Inserting input array to min heap   for (int i = 0; i < n; i++){      minHeap.push(arr[i]);   }      // Iterating backwards in the input array, where each element is replaced by the smallest element extracted from min heap   int i = n - 1;   while (!minHeap.empty()){      arr[i--] = minHeap.top();      minHeap.pop();   }}int main(){   int arr[6] = {5, 2, 9, 1, 5, 6};   int n = 6;   heapSort(arr, n);   cout << "Sorted array : ";   for (int i = 0; i < n; i++){      cout << arr[i] << " ";   }   cout << endl;   return 0;}

输出

Sorted array : 9 6 5 5 2 1

时间复杂度 – O(nlogn)

空间复杂度 – O(n)

方法2

该问题的另一个解决方案是从最后一个非叶根模式开始并向后构建一个最小堆。然后我们可以通过交换根节点和最后一个叶子节点来对数组进行排序,然后恢复最小堆属性。

伪代码

procedure heapify (arr[], n , i)   smallest = i   l = 2i + 1   r = 2i + 2   if l < n and arr[l] < arr[smallest]      smallest = l   end if   if r < n and arr[r] < arr[samllest]      smallest = r   end if   if smallest is not i      swap arr[i] to arr[smallest]      heapify (arr, n, smallest)   end ifend procedureprocedure heapSort (arr[], n)   for i = n/2 - 1 to 0      heapify(arr, n, i)   for i = n-1 to 0      swap arr[0] to arr[i]      heapify (arr, i, 0)end procedure

示例:C++ 实现

在下面的程序中,我们使用 heapify() 函数恢复以索引 i 为根的子树的最小堆属性,并使用 heapSort() 以相反的顺序构建最小堆。

#include using namespace std;// Restores the min heap property of subtree rooted at index ivoid heapify(int arr[], int n, int i){   int smallest = i;   int l = 2 * i + 1;   int r = 2 * i + 2;   if (l < n && arr[l] < arr[smallest]){      smallest = l;   }   if (r < n && arr[r] = 0; i--){      heapify(arr, n, i);   }      // Sort the array by repeatedly swapping the root node with the last leaf node   for (int i = n - 1; i >= 0; i--){      swap(arr[0], arr[i]);      heapify(arr, i, 0);   }}int main(){   int arr[6] = {5, 2, 9, 1, 5, 6};   int n = 6;   heapSort(arr, n);   cout << "Sorted array : ";   for (int i = 0; i < n; i++){      cout << arr[i] << " ";   }   cout << endl;   return 0;}

输出

Sorted array : 9 6 5 5 2 1

使用之前使用 heapSort() 函数创建最小堆的方法,我们可以在这个解决方案中使用相同的方法,但不是使用 heapify 来恢复最小堆的属性,而是使用传统的 hep 排序算法来创建 amin堆和排序元素的顺序递增,并进一步反转以获得所需的输出。

伪代码

procedure heapSort (arr[], n)   for i = n/2 - 1 to 0   parent = i      while parent *2+1 < n         child = parent*2+1         if child+1 arr[child+1]            child = child + 1         end if         if arr[parent] > arr[child]            swap arr[parent] to arr[child]            parent = child         else            break         end if   for i = n-1 to 0      swap arr[0] to arr[i]      parent = 0      while parent*2+1 < i            child = parent*2+1         if child+1 arr[child+1]            child = child + 1         end if         if arr[parent] > arr[child]            swap arr[parent] to arr[child]            parent = child         else            break         end ifend procedure

示例:C++ 实现

在下面的程序中,我们修改堆排序算法以降序对数组进行排序。

#include using namespace std;void heapSort(int arr[], int n){   // Building min heap in reverse order   for (int i = n / 2 - 1; i >= 0; i--) {         // Starting from last parent node, apply heapify operation      int parent = i;      while (parent * 2 + 1 < n) {         int child = parent * 2 + 1;         if (child + 1  arr[child + 1]){            child++;         }         if (arr[parent] > arr[child]){            swap(arr[parent], arr[child]);            parent = child;         }         else{            break;         }      }   }      // Extarct elekemnhts form min heap in decreasing order   for (int i = n - 1; i > 0; i--){      swap(arr[0], arr[i]);      int parent = 0;            // Perform heapify operation at new root node      while (parent * 2 + 1 < i){         int child = parent * 2 + 1;         if (child + 1  arr[child + 1]){            child++;         }         if (arr[parent] > arr[child]){            swap(arr[parent], arr[child]);            parent = child;         }         else {            break;         }      }   }}int main(){   int arr[6] = {5, 2, 9, 1, 5, 6};   int n = 6;   heapSort(arr, n);   cout << "Sorted array : ";   for (int i = 0; i < n; i++) {      cout << arr[i] << " ";   }   cout << endl;   return 0;}

输出

Sorted array : 9 6 5 5 2 1

结论

总之,为了使用最小堆执行降序堆排序,我们可以使用多种方法,其中一些方法的时间复杂度为 O(nlogn),每种方法的空间复杂度各不相同。

以上就是使用最小堆进行降序的堆排序的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1444031.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月17日 20:54:50
下一篇 2025年12月17日 20:55:04

相关推荐

  • 什么是堆排序?堆排序的实现步骤

    堆是一种特殊的完全二叉树,其中每个节点均大于(最大堆)或小于(最小堆)其子节点,堆排序通过构建和调整堆实现排序,首先将数组转化为最大堆,然后依次将堆顶最大值与末尾元素交换并重新堆化,直至有序;其时间复杂度为O(n log n),空间复杂度为O(1),属于原地不稳定排序,适用于大规模数据和内存受限环境…

    2025年12月20日
    000
  • 怎样在C++中实现堆排序_堆排序算法实现步骤解析

    堆排序是一种基于堆数据结构的原地排序算法,时间复杂度为o(n log n),空间复杂度为o(1)。其核心步骤包括:1. 构建最大堆;2. 将堆顶元素与末尾元素交换并调整堆。堆排序不稳定,因为在堆调整过程中相等元素的位置可能改变。相比快速排序,堆排序在最坏情况下的时间复杂度更优,但实际运行速度通常慢于…

    2025年12月18日 好文分享
    000
  • C++如何实现堆排序 C++堆排序的算法与代码解析

    堆排序的时间复杂度是o(n log n),空间复杂度是o(1)。1.构建堆的时间复杂度为o(n),2.每次调整堆的时间复杂度为o(log n),总共调整n-1次,3.空间复杂度为o(1)因为是原地排序,但递归调用会占用栈空间可忽略不计。优势包括时间复杂度稳定、原地排序节省空间;劣势包括实现较复杂、不…

    2025年12月18日 好文分享
    000
  • 如何使用C#编写堆排序算法

    如何使用C#编写堆排序算法 堆排序(Heap Sort)是一种基于完全二叉堆的排序算法,它的时间复杂度为O(nlogn)。在这篇文章中,我们将使用C#编写堆排序算法,并提供详细的代码示例。 建立堆 在堆排序算法中,首先需要构建一个最大堆(或最小堆)。最大堆的性质是父节点的值大于或等于其子节点的值,最…

    2025年12月17日
    000
  • C# 堆排序

    c#  堆排序 using System; using System.Collections; namespace Sort { public class HeapSorter { public static int[] Sort(int[] sortArray) { BuildMaxHeap(so…

    好文分享 2025年12月17日
    000
  • python堆排序是什么?

    堆排序是一种基于二叉堆的比较排序算法,先构建最大堆再逐个将堆顶最大值与末尾元素交换并调整堆,最终实现升序排列。 堆排序是一种基于比较的排序算法,它利用了二叉堆这种数据结构来实现。二叉堆本质上是一个完全二叉树,并且满足堆的性质:父节点的值总是大于或等于(最大堆)或小于或等于(最小堆)其子节点的值。 堆…

    2025年12月14日
    000
  • Python中如何实现堆排序?

    在python中实现堆排序的步骤是:1. 构建最大堆,从最后一个非叶子节点开始调整。2. 排序时,将堆顶元素与数组末尾元素交换,缩小堆并重新调整。堆排序的时间复杂度为o(n log n),但不是稳定排序,适合大规模数据。 def heapify(arr, n, i): largest = i; le…

    2025年12月14日
    000
  • 如何对PHP数组进行堆排序?

    堆排序在php中实现的步骤是:1. 构建最大堆;2. 逐一提取堆顶元素并调整堆。堆排序在处理大型数据集时高效,但在小数据集和需要保持元素顺序的场景下有局限性。 堆排序是一种高效的排序算法,尤其在处理大型数据集时表现出色。我们来看看如何在PHP中对数组进行堆排序,顺便分享一些我在实际项目中使用堆排序的…

    2025年12月10日
    000

发表回复

登录后才能评论
关注微信