Update 堆排序.cpp

master
pmkfqw2gu 4 years ago
parent abfef75d64
commit 744a0ec9cb

@ -1,5 +1,19 @@
#include <stdio.h>
5.
5.1
1(R1,R2.Rn)
2R[1]R[n](R1,R2,Rn-1)(Rn),
R[1,2n-1]<=R[n]
3R[1](R1,R2,Rn-1)
R[1](R1,R2.Rn-2)(Rn-1,Rn)
n-1
5.2
O(nlog2n)
O(1)
5.3
#include <stdio.h>
void swap(int arr[], int a, int b)
{
int tmp;
@ -14,21 +28,21 @@ void heapify(int tree[], int n, int i)
{
return;
}
int max = i;//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD>ֵ
int max = i;//假设父节点为最大值
int c1 = 2 * i + 1;
int c2 = 2 * i + 2; //<EFBFBD><EFBFBD><EFBFBD>ӽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>±<EFBFBD>Ϊ2i + 1, <20>Һ<EFBFBD><D2BA>ӽ<EFBFBD><D3BD><EFBFBD><EFBFBD>±<EFBFBD>Ϊ2i + 2
int c2 = 2 * i + 2; //左孩子结点的下标为2i + 1, 右孩子结点的下标为2i + 2
if (c1 < n && tree[c1] > tree[max])
{//<EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><EFBFBD>±<EFBFBD>ҪС<EFBFBD><EFBFBD><EFBFBD>ܽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
max = c1;//<EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD>ֵ<EFBFBD>Ľ<EFBFBD><EFBFBD><EFBFBD>±<EFBFBD><EFBFBD>¼<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{//左孩子的下标要小于总结点数
max = c1;//将较大值的结点下标记录下来
}
if (c2 < n && tree[c2] > tree[max])
{
max = c2;//<EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD>ֵ<EFBFBD>Ľ<EFBFBD><EFBFBD><EFBFBD>±<EFBFBD><EFBFBD>¼<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
max = c2;//将较大值的结点下标记录下来
}
if (max != i)
{//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD>Ǹ<EFBFBD><EFBFBD>ڵ<EFBFBD>
swap(tree, max, i);//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>tree[max]<5D><>tree[i]<5D><>ֵ
heapify(tree, n, max);//max<EFBFBD><EFBFBD><EFBFBD>Ǻ<EFBFBD><EFBFBD>ӽ<EFBFBD><EFBFBD><EFBFBD>±<EFBFBD>
{//如果最大值不是根节点
swap(tree, max, i);//交换tree[max]和tree[i]的值
heapify(tree, n, max);//max就是孩子结点下标
}
}
@ -48,7 +62,7 @@ void heapify_sort(int tree[], int n)
build_heapify(tree, n);
int tmp = tree[0];
for (int i = n - 1; i >= 0; i--)
{//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ
{//从最后一个结点开始
swap(tree, i, 0);
heapify(tree, i, 0);
}
@ -57,14 +71,14 @@ void heapify_sort(int tree[], int n)
int main(){
int tree[] = { 22, 34, 3, 32, 82, 55, 89, 50, 37, 5, 64, 35, 9, 70 };
int n = 14;
printf("ԭ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ:");
printf("原数组为:");
for (int i = 0; i < n; i++)
{
printf("%d ", tree[i]);
}
printf("\n");
heapify_sort(tree, n);
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>");
printf("经过堆排序后的数组为:");
for (int i = 0; i < n; i++)
{
printf("%d ", tree[i]);
@ -73,3 +87,5 @@ int main(){
return 0;
}
5.4
0.02498s
Loading…
Cancel
Save