标签:
package my0410; public class HeapSort { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int data[]={0,9,2,8,7,6,5,4,3,2,1,1,3,2}; heapSort(data); for(int i=0;i<data.length;i++){ System.out.println(data[i]); } } public static void heapSort(int[] data){ int i; for(i=data.length/2;i>0;i--){ heapAdjust(data,i,data.length-1); } for(i=data.length-1;i>1;i--){ swap(data,1,i); heapAdjust(data,1,i-1); } } public static void heapAdjust(int[] data,int s,int m){ int temp=data[s]; int j; for(j=2*s;j<=m;j*=2){ if(j<m&&data[j]<data[j+1]){ j+=1; } if(temp>data[j]) break; data[s]=data[j]; s=j; } data[s]=temp; } public static void swap(int[] data,int i,int j){ int temp=data[i]; data[i]=data[j]; data[j]=temp; } }
标签:
原文地址:http://www.cnblogs.com/qiaomu/p/4418512.html