数据结构初阶之二叉树——堆排序

目录

一. 建堆

二. 利用堆删除思想进行排序


一. 建堆

建堆有两种方法,一种是采用向上调整法,另一种是采用向下调整法。

以下是两种方法的代码实现:

//交换函数
inline void swap(int& a, int& b)
{
    int temp = a;
    a = b;
    b = temp;
}
//向上调整法
void AdjustUp(int (&arr) [13], int& child)
{
    int parent = (child - 1) / 2;
    //这里如果parent是大于0的话的就会少下标为0的元素
    while (parent >= 0)
    {
        if (arr[parent] < arr[child])
        {
            swap(arr[parent], arr[child]);
            child = parent;
            parent = (child - 1) / 2;
        }
        else
        {
            break;
        }
    }
}
//向下调整法
void AdjustDown(int (&arr) [13], int& n, int parent)
{
    int child = parent * 2 + 1;
    while (child < n)
    {
        if (arr[child] < arr[child + 1] && child + 1 < n)
        {
            child++;
        }

        if (arr[child] > arr[parent])
        {
            swap(arr[child], arr[parent]);
            parent = child;
            child = parent * 2 + 1;
        }
        else
        {
            break;
        }
    }
}

以上两种方法都可以进行建堆,但是他们的时间复杂度却是不一样的

可以通过以下方式证明:

如上图,最坏情况总的调整次数 = 每层数据个数 * 向上/下调整次数

  • 向上调整法时间复杂度计算

从上图可以发现:

第一层有2^0个结点

第二层有2^1个结点

第三层有2^2个结点

第四层有2^3个结点

第n层有2^(h-1)个结点

那么,建堆的次数(交换的次数)最坏的情况就是,第二层要交换两次,第三层要交换8次......

也就是说,向上调整的次数就会有:

Tn = 2^1*1 + 2^2*2 + 2^3*3 + ... + 2^(h-1)*(h-1)

到这里会发现这是一个等比乘等差的求和,即采用错位相减法,两边同时乘以2,就会有

2Tn = 2^2*1 + 2^3*2 + ... + 2^(h-1)*(h-2) + 2^h*(h-1)

2Tn - Tn 以后就会有:

Tn =  -2^1*1 - 2^2 - 2^3 - ... - 2^(h-1) + 2^h*(h-1)

把负号提出后就会变成:

Tn =  -(2^1 + 2^2 + 2^3 + ... + 2^(h-1))+ 2^h*(h-1)

为了方便等比求和就减去一个1再加上一个1,就会变成:

Tn =  -(2^0 + 2^1 + 2^2 + 2^3 + ... + 2^(h-1)) + 2^h*(h-1) + 1

前面等比求和以后就会变成:

Tn = 1 - 2^h + 2^h*(h-1) + 1

而二叉树的全部结点的和N就是2^0 + 2^1 + ... + 2^(h-1),也是等比数列求和,求和以后就是:

N = 2^h - 1

将该表达式代入Tn就会得到(N+1)(log2^(N+1)-1)+1,计算时间复杂度可以将常数去掉就变成了:

N*logN

所以向上调整法的时间复杂度是O(N*logN)

  • 向下调整法时间复杂度计算

这里使用向下调整法与以往不同的是需要从最后一个非叶子结点开始

因为使用向下调整法的前提是左右子树都必须是大堆或者小堆,否则无法使用,所以不再是从根结点开始调整,也就是从上图的第三层最后一个结点开始调整

从上图可以发现:

第一层有2^0个结点,需要向下调整h-1层

第二层有2^1个结点,需要向下调整h-2层

第三层有2^2个结点,需要向下调整h-3层

第四层有2^3个结点,需要向下调整h-4层

第h-1层有2^(h-1)个结点,需要向下调整1层

那么,建堆的次数(移动结点总的移动次数)最坏的情况就是,第一层要移动1*(h-1)步,第二层要移动2^1*(h-2)步,第三层要移动2^2*(h-3)步......

也就是说,向下调整的步数就会有:

Tn = 2^0*(h-1) + 2^1*(h-2) + 2^2*(h-3) + ... + 2^(h-2)*1

很明显,还是使用错位相减法,计算得出的最后结果是:

Tn = N - log(N+1)

时间复杂度是O(N)

通过计算发现向下调整法时间复杂度优于向上调整法

二. 利用堆删除思想进行排序

建好堆以后就是排序,排序需要注意,如果我们是想要升序排序就需要建大堆,反之建小堆。如果不这样做,向上调整法无法用来排序,只能使用向下调整法排序,而向下调整法会将堆打乱,拿完一个数据后需要重新建堆,建堆的时间复杂度为O(N),导致时间复杂度变成O(N^2)。

  • 升序:建大堆
inline void swap(int& a, int& b)
{
    int temp = a;
    a = b;
    b = temp;
}

void AdjustDown(int (&arr) [13], int& n, int parent)
{
    int child = parent * 2 + 1;
    while (child < n)
    {
        if (arr[child] < arr[child + 1] && child + 1 < n)
        {
            child++;
        }

        if (arr[child] > arr[parent])
        {
            swap(arr[child], arr[parent]);
            parent = child;
            child = parent * 2 + 1;
        }
        else
        {
            break;
        }
    }
}

void Test(int (&arr) [13], int& n)
{
    int parent = (n - 1 - 1) / 2;
    while (parent >= 0)
    {
        AdjustDown(arr, n, parent);
        --parent;
    }
    int end = n - 1;
    while (end > 0)
    {
        swap(arr[0], arr[end]);
        AdjustDown(arr, end, 0);
        --end;
    }
}
  • 降序:建小堆
inline void swap(int& a, int& b)
{
    int temp = a;
    a = b;
    b = temp;
}

void AdjustDown(int (&arr) [13], int& n, int parent)
{
    int child = parent * 2 + 1;
    while (child < n)
    {
        if (arr[child] > arr[child + 1] && child + 1 < n)
        {
            child++;
        }

        if (arr[child] < arr[parent])
        {
            swap(arr[child], arr[parent]);
            parent = child;
            child = parent * 2 + 1;
        }
        else
        {
            break;
        }
    }
}

void Test(int (&arr) [13], int& n)
{
    int parent = (n - 1 - 1) / 2;
    while (parent >= 0)
    {
        AdjustDown(arr, n, parent);
        --parent;
    }
    int end = n - 1;
    while (end > 0)
    {
        swap(arr[0], arr[end]);
        AdjustDown(arr, end, 0);
        --end;
    }
}

以下是完整的一个堆排序代码,该排序采用的升序:

inline void swap(int& a, int& b)
{
    int temp = a;
    a = b;
    b = temp;
}

void AdjustDown(int (&arr) [13], int& n, int parent)
{
    int child = parent * 2 + 1;
    while (child < n)
    {
        if (arr[child] < arr[child + 1] && child + 1 < n)
        {
            child++;
        }

        if (arr[child] > arr[parent])
        {
            swap(arr[child], arr[parent]);
            parent = child;
            child = parent * 2 + 1;
        }
        else
        {
            break;
        }
    }
}

void Test(int (&arr) [13], int& n)
{
    int parent = (n - 1 - 1) / 2;
    while (parent >= 0)
    {
        AdjustDown(arr, n, parent);
        --parent;
    }
    int end = n - 1;
    while (end > 0)
    {
        swap(arr[0], arr[end]);
        AdjustDown(arr, end, 0);
        --end;
    }
}

int main()
{

	int arr[] = { 2,3,1,6,4,76,34,65,24,12,0,22,11 };
    int num = sizeof(arr) / sizeof(arr[0]);
	Test(arr, num);
    //打印建好的堆
    for (int i = 0; i < sizeof(arr)/sizeof(arr[0]); i++)
    {
        cout << arr[i] << " ";
    }
    cout << endl;
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Hiland.

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

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

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

打赏作者

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

抵扣说明:

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

余额充值