From 744a0ec9cb6500c31d4e4bedfa28d860f5766121 Mon Sep 17 00:00:00 2001 From: pmkfqw2gu <2511089200@qq.com> Date: Thu, 17 Feb 2022 22:02:02 +0800 Subject: [PATCH] =?UTF-8?q?Update=20=E5=A0=86=E6=8E=92=E5=BA=8F.cpp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 堆排序.cpp | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/堆排序.cpp b/堆排序.cpp index a8cee20..98331e4 100644 --- a/堆排序.cpp +++ b/堆排序.cpp @@ -1,5 +1,19 @@ -#include +5.堆排序 + +5.1 描述 +(1)将初始待排序关键字序列(R1,R2….Rn)构建成大顶堆,此堆为初始的无序区; +(2)将堆顶元素R[1]与最后一个元素R[n]交换,此时得到新的无序区(R1,R2,……Rn-1)和新的有序区(Rn), + 且满足R[1,2…n-1]<=R[n]; +(3)由于交换后新的堆顶R[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 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;//���踸�ڵ�Ϊ���ֵ + int max = i;//假设父节点为最大值 int c1 = 2 * i + 1; - int c2 = 2 * i + 2; //���ӽ����±�Ϊ2i + 1, �Һ��ӽ����±�Ϊ2i + 2 + int c2 = 2 * i + 2; //左孩子结点的下标为2i + 1, 右孩子结点的下标为2i + 2 if (c1 < n && tree[c1] > tree[max]) - {//���ӵ��±�ҪС���ܽ���� - max = c1;//���ϴ�ֵ�Ľ���±��¼���� + {//左孩子的下标要小于总结点数 + max = c1;//将较大值的结点下标记录下来 } if (c2 < n && tree[c2] > tree[max]) { - max = c2;//���ϴ�ֵ�Ľ���±��¼���� + max = c2;//将较大值的结点下标记录下来 } if (max != i) - {//������ֵ���Ǹ��ڵ� - swap(tree, max, i);//����tree[max]��tree[i]��ֵ - heapify(tree, n, max);//max���Ǻ��ӽ���±� + {//如果最大值不是根节点 + 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--) - {//�����һ����㿪ʼ + {//从最后一个结点开始 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("ԭ����Ϊ:"); + printf("原数组为:"); for (int i = 0; i < n; i++) { printf("%d ", tree[i]); } printf("\n"); heapify_sort(tree, n); - printf("����������������Ϊ��"); + printf("经过堆排序后的数组为:"); for (int i = 0; i < n; i++) { printf("%d ", tree[i]); @@ -73,3 +87,5 @@ int main(){ return 0; } +5.4 运行时间 +0.02498s \ No newline at end of file