堆排序

基础知识:

       堆排序是利用这种数据结构而设计的一种排序算法,堆排序是一种选择排序它的最坏,最好,平均时间复杂度均为O(nlogn),它也是不稳定排序。首先简单了解下堆结构。
       堆是具有以下性质的完全二叉树每个结点的值都大于或等于其左右孩子结点的值,称为大顶堆;或者每个结点的值都小于或等于其左右孩子结点的值,称为小顶堆。如下图:

在这里插入图片描述

完全二叉树的特点:

已知当前节点的序号,能找出其父节点和左右孩子节点的序号。
在这里插入图片描述

堆排序的过程:

  1. 构建一个堆结构
  2. 每次把堆顶的元素和堆的最后一个元素交换,然后去除最后一个元素,这样又构成了一个堆结构。
  3. 重复1,2 过程。

如何构建一个堆 ( heapify的过程)

对于完全二叉树可以用一维数组来表示这棵完全二叉树,而完全二叉树又有它的特点,即根据当前节点可以找到左右孩子节点,也可以根据当前节点找到父节点。

指定任意一个节点的序列号i, 进行堆结构的过程:

void heapify(int tree[], int n, int i)
{
    if(i>=n)
    {
        return;
    }
    int child1 = 2*i+1;
    int child2 = 2*i+2;
    int max = i;
    if(child1 < n && tree[child1] > tree[max])
    {
        max = child1;
    }
    if(child2 < n && tree[child2] > tree[max])
    {
        max = child2;
    }
    if(i!=max)
    {
        swap_node(tree, max, i);
        heapify(tree, n, max);
    }
}

对于任何一个无序的数组,我们可以从底向上构建堆结构。因为节点的个数就是最后一个节点的序号,这样我们就知道了最后一个父节点的序号,如下图所示:
在这里插入图片描述

然后就可以整体构建堆结构了:

void build_heap(int tree[], int n)
{
    int last_node = n-1;
    int parent = (last_node-1)/2;
    int i;
    for(i = parent;i>=0;i--)
    {
        heapify(tree,n,i);
    }
}

构建堆结构的整体过程:

//
// Created by 彭程明 on 2019/6/20.
//

#include <iostream>
using namespace std;
void swap_node(int arr[], int i,int j)
{
    int temp;
    temp = arr[i];
    arr[i]=arr[j];
    arr[j]=temp;

}
void heapify(int tree[], int n, int i)
{
    int child1 = 2*i+1;
    int child2 = 2*i+2;
    int max = i;
    if(child1 < n && tree[child1] > tree[max])
    {
        max = child1;
    }
    if(child2 < n && tree[child2] > tree[max])
    {
        max = child2;
    }
    if(i!=max)
    {
        swap_node(tree, max, i);
        //cout<<max<<endl;
        heapify(tree, n, max);
    }
}
void build_heap(int tree[], int n)
{
    int last_node = n-1;
    int parent = (last_node-1)/2;
    int i;
    for(i = parent;i>=0;i--)
    {
        heapify(tree,n,i);
    }
}
int main()
{
    int tree[] = {2, 5, 3, 1, 10, 4 };
    int n = 6;
//    heapify(tree, n, 0);
    build_heap(tree, n);
    for (int i = 0;i<n;i++)
    {
        cout<<tree[i]<<endl;
    }
    return 0;
}

现在堆结构构建好了,我们就可以排序了,把每次堆顶的和堆结构的最后一个元素交换位置,然后再移除最后一个元素。

最后进行排序就好了

void heap_sort(int tree[], int n)
{
    build_heap(tree, n);
    for(int i=n-1;i>=0;i--)
    {
        swap_node(tree,i,0);
//        build_heap(tree, i);
        heapify(tree,i, 0);
    }

}

综上,堆排序整体代码如下:

//
// Created by 彭程明 on 2019/6/20.
//

#include <iostream>
using namespace std;
void swap_node(int arr[], int i,int j)
{
    int temp;
    temp = arr[i];
    arr[i]=arr[j];
    arr[j]=temp;

}
void heapify(int tree[], int n, int i)
{
    if(i>=n)
    {
        return;
    }
    int child1 = 2*i+1;
    int child2 = 2*i+2;
    int max = i;
    if(child1 < n && tree[child1] > tree[max])
    {
        max = child1;
    }
    if(child2 < n && tree[child2] > tree[max])
    {
        max = child2;
    }
    if(i!=max)
    {
        swap_node(tree, max, i);
        //cout<<max<<endl;
        heapify(tree, n, max);
    }
}
void build_heap(int tree[], int n)
{
    int last_node = n-1;
    int parent = (last_node-1)/2;
    int i;
    for(i = parent;i>=0;i--)
    {
        heapify(tree,n,i);
    }
}
void heap_sort(int tree[], int n)
{
    build_heap(tree, n);
    for(int i=n-1;i>=0;i--)
    {
        swap_node(tree,i,0);
//        build_heap(tree, i);
        heapify(tree,i, 0);
    }

}
int main()
{
    int tree[] = {1, 2, 3, 4, 5, 6};
    int n = 6;
//    heapify(tree, n, 0);
    heap_sort(tree, n);
    for (int i = 0;i<n;i++)
    {
        cout<<tree[i]<<endl;
    }
    return 0;
}

JavaScript代码实现:

function swap_node(tree, i, max) {
    let t = tree[i]
    tree[i] = tree[max]
    tree[max] = t;
}

function heapify(tree, n, i) {
    // let len = tree.length - 1
    if (i >= n) {
        return
    }
    let child1 = 2 * i + 1
    let child2 = 2 * i + 2;
    let max = i
    if (child1 < n && tree[max] < tree[child1]) {
        max = child1
    }
    if (child2 < n && tree[max] < tree[child2]) {
        max = child2
    }
    if (i != max) {
        swap_node(tree, i, max)
        heapify(tree, n, max)
    }
}

function build_heap(tree) {
    //找到父结点
    let last_node = tree.length - 1;
    let parent = parseInt((last_node - 1) / 2)
    for (let i = parent; i >= 0; i--) {
        heapify(tree, tree.length, i);
    }
}

function heap_sort(tree) {
    build_heap(tree)
    for (let i = tree.length - 1; i >= 0; i--) {
        swap_node(tree, i, 0);
        heapify(tree, i, 0)
    }
}
let arr = [3, 4, 2, 10, 100, 0]
heap_sort(arr)
console.log(arr)

python3 实现:

def heapify(tree, n, i):
    if i >= n:
        return
    child1 = 2 * i + 1
    child2 = 2 * i + 2
    max = i

    if child1 < n and tree[max] < tree[child1]:
        max = child1

    if child2 < n and tree[max] < tree[child2]:
        max = child2
    if max != i:
        tree[i], tree[max] = tree[max], tree[i]
        heapify(tree, n, max)


def build_heap(tree):
    n = len(tree)
    last_node = n - 1
    parent = (last_node - 1) // 2
    for i in range(parent, -1, -1):
        heapify(tree, n, i)


def heap_sort(tree):
    n = len(tree)
    build_heap(tree)
    for i in range(n - 1, 0, -1):
        tree[i], tree[0] = tree[0], tree[i]
        heapify(tree, i, 0)


arr = [1, 3, 4, 2, 5]
heap_sort(arr)
print(arr)

参考链接: https://www.bilibili.com/video/av47196993?from=search&seid=7155093278298159299

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

. . . . .

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值