标签:blog os io ar for div log amp ad
#include<stdio.h> void AdjustMinHeap(int *a,int pos,int len) { int temp,child; for(temp=a[pos];pos*2+1<=len;pos=child) { child=pos*2+1; if(child<len && a[child+1]<a[child]) child++; if(a[child]<temp) a[pos]=a[child]; else break; } a[pos]=temp; } void Swap(int &a,int &b) { int temp=a; a=b; b=temp; } void MyMinHeapSort(int *array,int len) { int i; for(i=len/2-1;i>=0;i--) AdjustMinHeap(array,i,len-1); for(i=len-1;i>=0;i--) { Swap(array[0],array[i]); AdjustMinHeap(array,0,i-1); } } int main() { int a[]={3,1,5,3,4,7,1}; MyMinHeapSort(a,7); for(int i=0;i<7;i++) printf("%d ",a[i]); return 0; }
标签:blog os io ar for div log amp ad
原文地址:http://www.cnblogs.com/zsboy/p/3946590.html