码迷,mamicode.com
首页 > 编程语言 > 详细

堆排序

时间:2016-04-20 23:51:30      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:

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);
		}
	}

}

  

堆排序

标签:

原文地址:http://www.cnblogs.com/abstract-fabulous/p/5414752.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!