我的Java MergeSort无法对某些阵列组合进行排序。第一个阵列排序很好,但是当我用6开6时,6在7之后结束。如果我用11切换9,那么我得到一个indexoutofboundsexception。任何建议或指导将不胜感激
import java.util.Arrays;
public class SortMerge {
public static void sort(int [] array, int beg, int end){
if(beg<end){
int mid =(beg+end)/2;
sort(array,beg,mid);
sort(array,mid+1,end);
mergesort(array);
}
}
public static void mergesort(int [] array){
int beg =0;
int end = array.length;
int mid = (beg+end)/2;
int second= mid+1;
int first =0;
int[] temp = new int [array.length];
for(int i=0;i<temp.length;i++){
if(first<=(mid) && second<=(end)){
if(array[first] <= array[second]){
temp[i] = array[first];
first++;
}else if(array[second]<array[first]){
temp[i] = array[second];
second++;
}
}else{
if(first>mid){
temp[i] = array[second];
second++;
}else if(second>end){
temp[i] = array[first];
first++;
}
}
}
for(int s=0; s<array.length; s++){
array[s] = temp[s];
}
}
public static void main(String [] args){
int [] one= {0,1,3,5,7,9,4,6,8,10};
int [] two= {0,1,3,5,7,6,4,6,8,10};
int [] three= {0,1,3,5,7,11,4,6,8,10};
sort(one,0,one.length-1);
System.out.println(Arrays.toString(one));
}
}