public static void merageSort(int[] array,int start,int end){
if(start>=end){
return;
}
int mid = (start+end)/2;
//对左边进行排序
merageSort(array,start,mid);
//对右边进行排序
merageSort(array,mid+1,end);
//两边排序好的进行合并
merage(array,start,mid,end);
}
public static void merage(int[] array,int start,int mid,int end){
int[] tmpArray=new int[array.length];
int tmpIndex = start;
int tmp = start;
int start2 = mid+1;
while (start<=mid&&start2<=end){
if(array[start]>array[start2]){
tmpArray[tmpIndex++] = array[start2++];
}else {
tmpArray[tmpIndex++] = array[start++];
}
}
while (start<=mid){
tmpArray[tmpIndex++] = array[start++];
}
while (start2<=end){
tmpArray[tmpIndex++] = array[start2++];
}
while (tmp<=end){
array[tmp]=tmpArray[tmp++];
}
}
JAVA归并排序
最新推荐文章于 2020-11-27 16:52:15 发布