代碼從一個文件中讀取一些情況和數組的大小,然後填充數組並將其發送到合併排序。
問題是我總是收到index out of bounds
,它正在殺死我......合併排序給出IndexOutOfBoundsException
我的調試器剛剛停止在我的eclipse上工作。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class mergesort {
public static void mergeSort(int[] array, int first, int last){
int mid;
if (first<last){
mid = (last + first)/2;
mergeSort(array, first, mid);
mergeSort(array, mid+1, last);
merge(array, first, last);
}
}
public static void merge(int[] array, int first, int last){
int mid = (last-first)/2;
int[] temp = new int[(last-first)];
int a1 = first, a2 = mid + 1, current = 0;
while (a1 <=mid && a2<=last){
if (array[a1] <= array[a2])
temp[current++] = array[a1++];
else
temp[current++] = array[a2++];
}
for (int i = a1; i<=mid; i++)
temp[current++] = array[i];
for (int i = a2; i<=last; i++)
temp[current++] = array[i];
for (int i =0; i<temp.length; i++)
array[first+i] = temp[i];
}
public static void main(String[] args) throws FileNotFoundException {
File file = new File("sort.in");
Scanner scan = new Scanner(file);
int n1 = scan.nextInt();
for (int i = 0; i<n1; i++){
int[] array =new int[scan.nextInt()];
for (int j = 0; j<array.length; j++){
array[j] = scan.nextInt();
}
mergeSort(array, 0, (array.length)-1);
for (int j = 0; j<array.length; j++){
System.out.println(array[j]);
}
}
}
}
*「和調試器不上我的日食工作......」 *所以第一步是修復您的Eclipse安裝。 – 2013-02-27 17:48:06
第二步是向我們展示堆棧跟蹤,以及您在哪裏得到的AIOOB – pcalcao 2013-02-27 17:49:32