标签:style blog color ar for sp div c log
package sort; public class CheapSort { public static final int MAX=10; public static void main(String[] args){ int []a={0,16,7,3,20,17,8}; int len=a.length; buildHeap(a,len-1); for(int i=1; i<len; i++) System.out.print(a[i]+","); sort(a,len-1); System.out.println(); for(int i=1; i<len; i++) System.out.print(a[i]+","); } public static void sort(int[] a,int size){ int n=size-1; if(n==1) return; swap(a,1,size); adjustHeap(a,1,n); sort(a,n); } public static void swap(int[] a,int i,int j){ int temp; temp=a[i]; a[i]=a[j]; a[j]=temp; } public static void buildHeap(int[] a,int size){ for(int i=size/2; i>0; i--) adjustHeap(a,i,size); } private static void adjustHeap(int[] a, int i, int size) { // TODO Auto-generated method stub int lchild=i*2; int rchild=i*2+1; int max=i; if(lchild<size && a[lchild]>a[max]) max=lchild; if(rchild<size && a[rchild]>a[max]) max=rchild; if(max!=i) { swap(a,i,max); adjustHeap(a,max,size); } } }
标签:style blog color ar for sp div c log
原文地址:http://www.cnblogs.com/huangcongcong/p/4004142.html