package my0410;
public class MergeSort {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int a[]={9,8,7,6,5,4,3,2,1};
int[] aux = (int[])a.clone();
mergeSort(aux, a,0,a.length);
for(int i=0;i<a.length;i++){
System.out.println(a[i]);
}
}
//low到high-1,不包括high
public static void mergeSort(int []data,int []temp,int low,int high){
if(low==high-1){//注意边界条件
return;
}
int mid=(low+high)/2;
mergeSort(temp,data,low,mid);
mergeSort(temp,data,mid,high);
int i,p=low,q=mid;
for(i=low;p<mid&&q<high;i++){
if(data[p]<data[q]){
temp[i]=data[p++];
}else{
temp[i]=data[q++];
}
}
while(p<mid&&i<high){
temp[i++]=data[p++];
}
while(q<high&&i<high){
temp[i++]=data[q++];
}
}
}