堆排序

package Sort;

public class MergeSort {
	public  int[] sort(int a[],int low,int high){
		int m=(low+high)/2;
		if(low<high){
			sort(a,low,m);
			sort(a,m+1,high);
			Merge(a,low,m,high);
		}
		return  a;
	}

	private void Merge(int[] a, int low, int m, int high) {
		// TODO Auto-generated method stub
		int temp[]=new int[high-low+1];
		int i,j,k;
		i=low;
		j=m+1;
		//k=0;
		for(k=0;i<=m&&j<=high;k++){
//		while(i<=m&&j<=high){
			if(a[i]<=a[j]){
				temp[k]=a[i++];
			}
			else{
				temp[k]=a[j++];
			}
		}
		while(i<=m){
			temp[k++]=a[i++];
		}
		while(j<=high){
			temp[k++]=a[j++];
		}
        for(k=0,i=low;i<=high;i++,k++){
        	a[i]=temp[k];
        }
	}
	public static void main(String args[]){
		int a[]={8,2,5,9,0,1,4,2,3};
		int b[]=new MergeSort().sort(a,0,a.length-1);
		for(int i:b){
			System.out.print(i);
		}
	}

}

  

posted on 2016-04-20 22:24  笑傲江湖未平生  阅读(122)  评论(0)    收藏  举报