简体   繁体   中英

Keep getting java.lang.ArrayIndexOutOfBoundsException: 5 for my mergesort implementation

I'm trying to implement mergesort without looking at any source code. Whenever I try run my program I get this exception:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at Mergesort.merge_halves(Mergesort.java:40)
at Mergesort.mergesort(Mergesort.java:31)
at Mergesort.mergesort(Mergesort.java:29)
at Mergesort.main(Mergesort.java:15)

Here is my program:

public class Mergesort {

private static int [] tempArr;

public static void main(String [] args) {
    int [] arr = new int[5];

    arr[0] = 2;
    arr[1] = 4;
    arr[2] = 9;
    arr[3] = 11;
    arr[4] = 3;

    Mergesort sorter = new Mergesort();
    sorter.mergesort(arr, arr[0], arr.length - 1);

    for(int i = 0; i < tempArr.length - 1; i++) {
        System.out.print(tempArr[i] + " ");
    }
}

public void mergesort(int [] arr, int low, int high) {
    if(low >= high) {
        return;
    }

    int mid = (low + high) / 2;

    mergesort(arr, low, mid);
    mergesort(arr, mid + 1, high);
    merge_halves(arr, low, mid, high);
}

public void merge_halves(int [] arr, int low, int mid, int high) {
    tempArr = new int[arr.length];

    int i = 0;

    while(i < tempArr.length) {
        if(arr[i] <= arr[i + 1]) {
            tempArr[i] = arr[i];
            i++;
        } else {
            tempArr[i] = arr[i + 1];
            i++;
        }
      }
   }
}

I have tried to fix the error by changing the condition in the while loop to make the program run correctly

while(i < tempArr.length - 1)

but when I compile this and print the tempArr, the array I get back is missing an element:

2 4 9 3

How do I fix this?

Not sure if that's the only error, but you are passing the first number of the array instead of the first index of the array in the first call to mergeSort .

Change

sorter.mergesort(arr, arr[0], arr.length - 1);

to

sorter.mergesort(arr, 0, arr.length - 1);

Actually merge_halves also has a problem. arr[i + 1] will cause an ArrayIndexOutOfBoundsException when i == tempArr.length - 1.

The missing element comes from the way you print: You explicitly loop up to array.length - 1.

Your merge function doesn't merge anything. You should first copy arr to temp and then assembe the elements in order in arr again. (Or assemble them in temp and then copy back to arr, of course.) Your working array should always be arr.

You also don't do the merger correctly. You have three arrays: the lower sorted array, 'a', the upper sorted array, 'b' and the result array ' r', which should also be sorted, of course.

At the beginning a and b are in arr next to each other with a in arr[low ... mid] and b in arr[mid ... high] . (Here, arr[i ... j] means the subarray from index i up to, but not containing, j .)

After the merge, arr should contain r .

You compare two adjacent array elements arr[i] and arr[i + 1] , but you should compare the current element of a to the current element of b until you have exhausted one of the arrays. So you need three indices, one for each array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM