归并排序
package sort;
/**
* Create by ~JH~ on 2018/4/27
*/
public class MergeSort {
public static int[] sort(int [] a,int low,int high){
int mid=(low+high)/2;
if (low<high){
sort(a,low,mid);
sort(a,mid+1,high);
merge(a,low,mid,high);
}
return a;
}
public static void merge(int []a,int low,int mid,int high){
int[]temp=new int[high-low+1];
int i=low,k=0;
int j=mid+1;
for (;i<=mid&&j<=high;){
if (a[i]<a[j]){
temp[k++]=a[i++];
}else{
temp[k++]=a[j++];
}
}
while(i<=mid){
temp[k++]=a[i++];
}
while(j<=high){
temp[k++]=a[j++];
}
for (int m=0;m<k;m++){
a[low+m]=temp[m];
}
}
public static void main(String[] args) {
int []a={2,4,6,3,8,7,1,0,5};
sort(a,0,a.length-1);
for (int i:a){
System.out.print(i);
}
}
}