基础排序之堆排序(C语言小堆版)
堆排序
#include<stdio.h> typedef int ElementType; int arr1[11]={0,2,87,39,49,34,62,53,6,44,98}; void Swap(int* a,int* b) { int temp=*a; *a=*b; *b=temp; } void PercDown(int A[], int i, int N) { int child; ElementType Tmp; for (Tmp = A[i]; 2*i+1 < N; i = child){ child = 2*i+1; //注意数组下标是从0开始的,所以左孩子的求发不是2*i if (child != N - 1 && A[child + 1] > A[child]) ++child; //找到最大的儿子节点 if (Tmp < A[child]) A[i] = A[child]; else break; } A[i] = Tmp; } void HeapSort(int A[], int N) { int i; for (i = N / 2; i >= 0; --i) PercDown(A, i, N); //构造堆 for(i=N-1;i>0;--i) { Swap(&A[0],&A[i]); //将最大元素(根)与数组末尾元素交换,从而删除最大元素,重新构造堆 PercDown(A, 0, i); } } void Print(int A[],int N) { int i; for(i=0;i<N;i++) { printf(" %d ",A[i]); } } int main() { int arr[10]={2,87,39,49,34,62,53,6,44,98}; Print(arr,10); printf("\n"); HeapSort(arr,10); Print(arr,10); printf("\n"); return 0; }
相关推荐
ELEMENTS爱乐小超 2020-07-04
sunjunior 2020-05-10
shenwenjie 2020-05-03
shawsun 2020-04-17
ustbfym 2020-04-09
rein0 2020-04-08
shawsun 2020-03-28
hanyujianke 2020-01-04
shawsun 2019-12-15
ustbfym 2019-12-09
Happyunlimited 2019-12-02
燕哥带你学算法 2019-11-19
qscool 2019-11-08
nimeijian 2019-10-21
sxyyu 2019-10-21
ScarlettYellow 2018-03-06