diff --git a/堆排序.cpp.cpp b/堆排序.cpp.cpp deleted file mode 100644 index a8cee20..0000000 --- a/堆排序.cpp.cpp +++ /dev/null @@ -1,75 +0,0 @@ -#include - -void swap(int arr[], int a, int b) -{ - int tmp; - tmp = arr[a]; - arr[a] = arr[b]; - arr[b] = tmp; -} - -void heapify(int tree[], int n, int i) -{ - if (i >= n) - { - return; - } - int max = i;//���踸�ڵ�Ϊ���ֵ - int c1 = 2 * i + 1; - int c2 = 2 * i + 2; //���ӽ����±�Ϊ2i + 1, �Һ��ӽ����±�Ϊ2i + 2 - if (c1 < n && tree[c1] > tree[max]) - {//���ӵ��±�ҪС���ܽ���� - max = c1;//���ϴ�ֵ�Ľ���±��¼���� - } - if (c2 < n && tree[c2] > tree[max]) - { - max = c2;//���ϴ�ֵ�Ľ���±��¼���� - } - if (max != i) - {//������ֵ���Ǹ��ڵ� - swap(tree, max, i);//����tree[max]��tree[i]��ֵ - heapify(tree, n, max);//max���Ǻ��ӽ���±� - } -} - -void build_heapify(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 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); - } -} - -int main(){ - int tree[] = { 22, 34, 3, 32, 82, 55, 89, 50, 37, 5, 64, 35, 9, 70 }; - int n = 14; - printf("ԭ����Ϊ:"); - for (int i = 0; i < n; i++) - { - printf("%d ", tree[i]); - } - printf("\n"); - heapify_sort(tree, n); - printf("����������������Ϊ��"); - for (int i = 0; i < n; i++) - { - printf("%d ", tree[i]); - } - printf("\n"); - return 0; -} -