4

代码从文件中读取多个案例和数组的大小,然后填充一个数组并将其发送到合并排序。
问题是我不断得到index out of bounds它,它正在杀死我......

我的调试器刚刚停止在我的 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]); 
            }
        }
    }
}
4

2 回答 2

1

对于初学者,您的temp数组是一个太短的元素:

int[] temp = new int[(last-first)];

由于lastfirst都包含在内,以上内容应为:

int[] temp = new int[last - first + 1];
于 2013-02-27T17:54:31.870 回答
1

您的merge程序部分有几个错误(不包括 NPE 显示的错误),首先:

int mid = (last-first)/2;

应该:

int mid = (last+first)/2;

其次,不管怎样,最后的 3 个 for 循环都在运行 - 但您应该只在 a1/a2 的剩余部分上运行。

我的实现有点不同 - 也许它有助于解释第二个错误:

public static void mergeSort(int[] arr, int start, int end){
    if(start == end) return;
    // now the recursive calls:
    // divide the arr:
    int middle = (end+start)/2;
    mergeSort(arr, start, middle);
    mergeSort(arr, middle+1, end);
    // now merge (conquer)
    merge(arr, start, end, middle);
}

private static void merge(int[] arr, int start, int end, int middle) {
    int[] sorted = new int[end-start+1];        
    for(int i=start,j=middle+1,index=0; index < sorted.length;){
        if(j<=end && arr[i] > arr[j]){
            sorted[index] = arr[j];
            j++; index++;
        }
        else if (i<=middle){
            sorted[index] = arr[i];
            i++; index++;
        }
        else{
            sorted[index] = arr[j];
            j++; index++;
        }
    }
    // now copy "sorted" to original array
    for(int i=start,j=0; i<=end; i++,j++){
        arr[i] = sorted[j];
    }
}

public static void main(String...args){
    int[] arr = {4,2,9,6,2,8,1,9,10,15,12,11};
    mergeSort(arr, 0, arr.length - 1);
    for(int i=0; i<arr.length; i++){
        System.out.print(arr[i]+" ");
    }
}
于 2013-08-06T05:15:51.893 回答