hdu 1040 As Easy As A+B(堆排练习水题~)

堆排写得没有快排顺利 = = 。。。等隔一段时间再写试试

 

 

#include <stdio.h> #include <stdlib.h> #include <iostream> #include <memory.h> using namespace std; int n; void HeapMin(int a[],int i,int heapsize) { int larg; int l = i*2, r = i*2+1; if( l <= heapsize && a[i] <= a[l] ) larg = l; else larg = i; if( r <= heapsize && a[larg] <= a[r] ) larg = r; if( larg != i ) { swap(a[larg],a[i]); HeapMin(a,larg,heapsize); } } void HeapBuild(int a[],int heapsize) { for(int i=heapsize/2; i>=1; i--) HeapMin(a,i,heapsize); } void HeapSort(int a[],int heapsize) { HeapBuild(a,heapsize); for(int i=n; i>=2; i--) { swap(a[1],a[i]); heapsize--; HeapMin(a,1,heapsize); } } int main(void) { int ncases; int a[1001]; scanf("%d",&ncases); while( ncases-- ) { scanf("%d",&n); for(int i=1; i<=n; i++) scanf("%d",&a[i]); HeapSort(a,n); for(int i=1; i<n; i++) printf("%d ",a[i]); printf("%d",a[n]); printf("/n"); } return 0; }

 

 

你可能感兴趣的:(hdu 1040 As Easy As A+B(堆排练习水题~))