堆排序

来源:互联网 发布:linux wput 编辑:程序博客网 时间:2024/06/05 00:46
void HeapAdjust(int a[], int pos, int length){int child = 2 * pos + 1;int temp;for(int p = pos; child<length; child = 2 * p + 1){if((child+1)<length && a[child+1]>a[child])++child;if(a[p]<a[child]){temp = a[p];a[p] = a[child];a[child] = temp;}elsebreak;p = child;}}void HeapSort(int a[], int length){int n = (length - 1)/2;for(;n>=0;--n){HeapAdjust(a,n,length);}while(length>1){int temp = a[0];a[0] = a[length - 1];a[length - 1] = temp;--length;HeapAdjust(a,0,length);}}

0 0
原创粉丝点击