目录
一. 建堆
建堆有两种方法,一种是采用向上调整法,另一种是采用向下调整法。
以下是两种方法的代码实现:
//交换函数
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;
}