目录
问题陈述
示例 2
方法2
伪代码
示例:C++ 实现
输出
结论
首页 后端开发 C++ 使用最小堆进行降序的堆排序

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

Aug 29, 2023 pm 03:49 PM
堆排序 降序 最小堆

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

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

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

问题陈述

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

示例 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 minHeap
end procedure
登录后复制

示例:C++ 实现

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

#include <bits/stdc++.h>
using namespace std;
// Function to heap sort in decreasing order using min heap
void heapSort(int arr[], int n){

   // Creating min heap using a priority queue
   priority_queue<int, vector<int>, greater<int> > 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 if
end procedure

procedure 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 <bits/stdc++.h>
using namespace std;

// Restores the min heap property of subtree rooted at index i
void 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] < arr[smallest]){
      smallest = r;
   }
   if (smallest != i){
      swap(arr[i], arr[smallest]);
      heapify(arr, n, smallest);
   }
}
void heapSort(int arr[], int n){

   // Build the min heap in reverse order
   for (int i = n / 2 - 1; i >= 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 < n and arr[child] >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 < n and arr[child] >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
end procedure
登录后复制

示例:C++ 实现

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

#include <bits/stdc++.h>
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 < n && arr[child] > 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 < i && arr[child] > 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),每种方法的空间复杂度各不相同。

以上是使用最小堆进行降序的堆排序的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Java教程
1662
14
CakePHP 教程
1418
52
Laravel 教程
1311
25
PHP教程
1261
29
C# 教程
1234
24
如何使用C#编写堆排序算法 如何使用C#编写堆排序算法 Sep 19, 2023 am 08:45 AM

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

如何使用java实现堆排序算法 如何使用java实现堆排序算法 Sep 19, 2023 pm 06:00 PM

如何使用Java实现堆排序算法堆排序是一种基于堆数据结构的排序算法,它利用了堆的性质来进行排序。堆排序分为两个主要步骤:建堆和排序。建堆:首先,我们需要根据待排序的数组构建一个大根堆或小根堆。对于升序排序,我们需要构建一个大根堆;对于降序排序,我们需要构建一个小根堆。大根堆的性质是:节点的值大于或等于其子节点的值。小根堆的性质是:节点的值小于或等于其子节点的

使用最小堆进行降序的堆排序 使用最小堆进行降序的堆排序 Aug 29, 2023 pm 03:49 PM

堆排序-堆排序是一种基于比较的算法,它使用二叉树数据结构按升序或降序对数字列表进行排序。它通过堆排序创建一个堆数据结构,其中根是最小元素,然后删除根,再次排序在根位置给出列表中第二小的数字。最小堆-最小堆是一种数据结构,其中父节点始终小于子节点,因此根节点是所有元素中最小的元素。问题陈述给定一个整数数组。使用最小堆按降序对它们进行排序。示例1Input:[2,5,1,7,0]Output:[7,5,2,1,0]示例2Input:[55,1,23,10,1]Output:[55,23,10,1,1

C++程序:将数组元素按降序排序 C++程序:将数组元素按降序排序 Sep 09, 2023 pm 07:09 PM

在解决一些问题时,以适当的形式排列数据项是一项重要的任务。efficientway.Theelementsortingproblemisoneofthemostcommonlydiscussed排列问题。在本文中,我们将看到如何排列数组元素按照它们的值降序排列(在C++中)。在这个领域中有许多不同的排序算法用于对数字或非数字进行排序按给定顺序的元素。在本文中,我们将只介绍两种简单的方法sorting.Thebubblesortandtheselectionsort.Letusseethemone

Java程序以降序对数组元素进行排序 Java程序以降序对数组元素进行排序 Aug 27, 2023 pm 06:41 PM

数组是存储在某些连续内存位置的相同数据类型的集合。数组是java.until包中的一个类,它以静态方式提供预定义的排序,并且没有返回值。这是下面提到的Arrays.sort()方法的语法-publicstaticvoidsort(int[]ar,intfrom_index,intto_index)在上面的语法中,我们有ar-数组名称的缩写from_index-我们可以将其用作可选参数,其中排序需要运行。to_index-可选参数提供元素的索引。这是一个例子Input:Array={2,6,23,

如何使用PHP编写堆排序算法 如何使用PHP编写堆排序算法 Jul 08, 2023 pm 07:13 PM

如何使用PHP编写堆排序算法堆排序是一种高效的排序算法,它的核心思想是将待排序的序列构建成一个二叉堆,然后通过不断调整堆的结构来实现排序。本文将介绍如何使用PHP编写堆排序算法,并提供代码示例供参考。堆的定义在开始编写堆排序算法之前,首先需要明确堆的定义和性质。堆是一个具有以下性质的完全二叉树:对于任意节点i,满足以下两个条件:父节点的值总是大于或等于子节点

学习PHP中堆排序算法的原理及时间复杂度分析。 学习PHP中堆排序算法的原理及时间复杂度分析。 Sep 19, 2023 am 11:12 AM

学习PHP中堆排序算法的原理及时间复杂度分析堆排序是一种基于堆数据结构的排序算法,它的时间复杂度为O(nlogn)。本文将介绍PHP语言中堆排序算法的原理,同时提供代码示例。一、堆的定义和性质在学习堆排序之前,首先需要了解堆的定义和性质。堆是一种完全二叉树,其每一个节点的值都大于或等于其子节点的值,我们把这样的堆称之为大顶堆。相反,如果每一个节点的值都小于或

Java错误:堆排序错误,如何处理和避免 Java错误:堆排序错误,如何处理和避免 Jun 24, 2023 pm 11:17 PM

随着计算机科学的不断发展,Java成为了现代软件开发中最重要的编程语言之一。然而,在编写Java程序时,我们时常会遇到各种各样的错误和问题。其中,堆排序错误在Java编程中是比较常见的问题之一。那么,当出现堆排序错误时,我们应该如何处理和避免呢?1.什么是堆排序?堆排序是一种比较常用的排序算法,它能在O(n*logn)时间复杂度内实现排序。堆排序使用了一种称

See all articles