5
我正在打印2行到控制檯。它們都可以打印,但是當第二個打印時,第一個打印到第二個,所以這兩行是相同的。我從來沒有遇到過這個。爲什麼第二次打印覆蓋第一張,我該如何解決?系統輸出println
public static void main(String args[]){
new MergeSort(90000);
System.out.println("Array to be mergesorted: " +Arrays.toString(array));
long start = System.currentTimeMillis();
mergeSort(array, 1, array.length);
long end = System.currentTimeMillis();
System.out.println("Result: " + Arrays.toString(array));
}
構造:
public MergeSort(int n){
Random rand = new Random();
array = new int[n];
for(int i = 0; i <array.length; i++){
array[i] = rand.nextInt(101);
}
}
休息的代碼:
public static void merge(int[] A, int p, int q, int r){
//
//length of subarray 1
int n1 = q-p+1;
//length of subarray 2
int n2 = r-q;
int[] L = new int[n1+1];
int[] R = new int[n2+1];
for(int i = 0; i < n1; i++){
L[i] = A[p+i-1];
}
for(int j=0; j< n2; j++){
R[j] = A[q+j];
}
L[n1] = Integer.MAX_VALUE;
R[n2] = Integer.MAX_VALUE;
int i = 0;
int j = 0;
for(int k = p-1; k < r; k++){
if(L[i] <= R[j]){
A[k] = L[i];
i++;
}
else{
A[k] = R[j];
j++;
}
}
}
public static void mergeSort(int[] A, int p, int r){
if (p<r){
int q = (int) Math.floor((r+p)/2);
mergeSort(A, p, q);
mergeSort(A, q+1, r);
merge(A, p, q, r);
}
}
你的'MergeSort'構造函數用'new MergeSort(90000)'做了什麼?而且,你的'mergeSort(array,1,array.length)'指令是做什麼的? –
@ElliottFrisch您請求的代碼現在已添加。 – Jesper
我*建議*你嘗試一個調試器。 –