堆排序算法过程图解_堆排序算法

堆排序算法过程图解

Heap Sort is one of the best sorting methods being in-place and with no quadratic worst-case running time. Heap sort involves building a Heap data structure from the given array and then utilizing the Heap to sort the array.

堆排序是就位的最佳排序方法之一,没有二次最坏情况的运行时间。 堆排序涉及从给定的数组构建数据结构,然后利用堆对数组进行排序。

You must be wondering, how converting an array of numbers into a heap data structure will help in sorting the array. To understand this, let's start by understanding what is a Heap.

您一定想知道,将数字数组转换为堆数据结构将如何帮助对数组进行排序。 要了解这一点,让我们从了解什么是堆开始。

什么是堆? (What is a Heap ?)

Heap is a special tree-based data structure, that satisfies the following special heap properties:

堆是一种特殊的基于树的数据结构,它满足以下特殊的堆属性:

  1. Shape Property: Heap data structure is always a Complete Binary Tree, which means all levels of the tree are fully filled.

    形状属性:堆数据结构始终是完整的二叉树,这意味着树的所有级别都已完全填充。

    difference between complete and incomplete binary tree
  2. Heap Property: All nodes are either greater than or equal to or less than or equal to each of its children. If the parent nodes are greater than their child nodes, heap is called a Max-Heap, and if the parent nodes are smaller than their child nodes, heap is called Min-Heap.

    堆属性:所有节点都大于或等于小于或等于其每个子节点。 如果父节点大于其子节点,则堆称为Max-Heap ;如果父节点小于其子节点,则堆称为Min-Heap

    Min-Heap and Max-heap

堆排序如何工作? (How Heap Sort Works?)

Heap sort algorithm is divided into two basic parts:

堆排序算法分为两个基本部分:

  • Creating a Heap of the unsorted list/array.

    创建未排序列表/数组的堆。

  • Then a sorted array is created by repeatedly removing the largest/smallest element from the heap, and inserting it into the array. The heap is reconstructed after each removal.

    然后,通过从堆中反复删除最大/最小元素,然后将其插入到数组中来创建排序后的数组。 每次删除后都将重建堆。

Initially on receiving an unsorted list, the first step in heap sort is to create a Heap data structure(Max-Heap or Min-Heap). Once heap is built, the first element of the Heap is either largest or smallest(depending upon Max-Heap or Min-Heap), so we put the first element of the heap in our array. Then we again make heap using the remaining elements, to again pick the first element of the heap and put it into the array. We keep on doing the same repeatedly untill we have the complete sorted list in our array.

最初在接收到未排序的列表时,堆排序的第一步是创建一个堆数据结构(Max-Heap或Min-Heap)。 一旦构建了堆,堆的第一个元素就是最大或最小(取决于最大堆或最小堆),因此我们将堆的第一个元素放入数组中。 然后,我们再次使用剩余的元素制作堆,以再次选择堆的第一个元素并将其放入数组中。 我们继续重复进行同样的操作,直到数组中有完整的排序列表为止。

In the below algorithm, initially heapsort() function is called, which calls heapify() to build the heap.

在下面的算法中,最初会调用heapsort()函数,该函数将调用heapify()来构建堆。

实现堆排序算法 (Implementing Heap Sort Algorithm)

Below we have a simple C++ program implementing the Heap sort algorithm.

下面我们有一个简单的C ++程序,实现了堆排序算法。

/*  Below program is written in C++ language  */

#include <iostream>

using namespace std;

void heapify(int arr[], int n, int i)
{
    int largest = i;
    int l = 2*i + 1;
    int r = 2*i + 2;
 
    // if left child is larger than root
    if (l < n && arr[l] > arr[largest])
        largest = l;
 
    // if right child is larger than largest so far
    if (r < n && arr[r] > arr[largest])
        largest = r;
 
    // if largest is not root
    if (largest != i)
    {
        swap(arr[i], arr[largest]);
 
        // recursively heapify the affected sub-tree
        heapify(arr, n, largest);
    }
}

void heapSort(int arr[], int n)
{
    // build heap (rearrange array)
    for (int i = n / 2 - 1; i >= 0; i--)
        heapify(arr, n, i);
 
    // one by one extract an element from heap
    for (int i=n-1; i>=0; i--)
    {
        // move current root to end
        swap(arr[0], arr[i]);
 
        // call max heapify on the reduced heap
        heapify(arr, i, 0);
    }
}
 
/* function to print array of size n */
void printArray(int arr[], int n)
{
    for (int i = 0; i < n; i++)
    {
        cout << arr[i] << " ";
    }
    cout << "\n";
}
 

int main()
{
    int arr[] = {121, 10, 130, 57, 36, 17};
    int n = sizeof(arr)/sizeof(arr[0]);
 
    heapSort(arr, n);
 
    cout << "Sorted array is \n";
    printArray(arr, n);
}

堆排序的复杂度分析 (Complexity Analysis of Heap Sort)

Worst Case Time Complexity: O(n*log n)

最坏情况下的时间复杂度: O(n * log n)

Best Case Time Complexity: O(n*log n)

最佳情况下时间复杂度: O(n * log n)

Average Time Complexity: O(n*log n)

平均时间复杂度: O(n * log n)

Space Complexity : O(1)

空间复杂度: O(1)

  • Heap sort is not a Stable sort, and requires a constant space for sorting a list.

    堆排序不是稳定排序,并且需要恒定的空间来对列表进行排序。

  • Heap Sort is very fast and is widely used for sorting.

    堆排序非常快,被广泛用于排序。

翻译自: https://www.studytonight.com/data-structures/heap-sort

堆排序算法过程图解

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值