Julie Morris
Julie Morris

Reputation: 15

How to fix ArrayIndexOutOfBoundsException for a Merge sort method?

So the problem that I am running into is that I am trying to get my merge sort implementation to run, but I keep getting an Exception error that says the Array index is out of bounds. This is a runtime error because I am able to compile the program with no issues and it will run up until it hits my merge sort call. One thing that I tried was changing one of my variables to match the other within the merge method (int k = 0; //line 39). When I did this, the code ran, however, the merge sorted array was not correct. I even tried debugging the code, but couldn't see an issue with it. Below is my code:

public static void merge_sort(int A[], int l, int r){

 if(l < r){
    int m = (l + r)/2;
    merge_sort(A, l, m);
    merge_sort(A, m + 1, r);
    merge(A, l, m, r);//Line17
  }
 }

  public static void merge(int A[], int l, int m, int r){


  int n1 = m - l + 1;
  int n2 = r - m;

  int L[] = new int [n1];
  int R[] = new int [n2];

  for(int i = 0; i < n1; i++){
     L[i] = A[l + i];
  }
  for(int j = 0; j < n2; j++){
     R[j] = A[m + 1 + j];
  }

 int i = 0;
 int j = 0;
 int k = 1; //line39

  while(i < n1 && j < n2){
     if(L[i] <= R[j]){
        A[k] = L[i];
        i++;
     }
     else{
        A[k] = R[j];
        j++;
     }
     k++;
  }

  while(i < n1){
     A[k] = L[i];
     i++; 
     k++;
   }

   while(j < n2){
     A[k] = R[j]; //line60
     j++;
     k++;
   }
}

And here is the error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 15
at HW3.merge(HW3.java:60)
at HW3.merge_sort(HW3.java:17)
at HW3.main(HW3.java:160) //this line is where I call the method within the main

I understand that this means that the array is going out of the set size of 15 but I am unsure of how to fix this issue. I have tried looking at similar problems, but I did not see a solution to the issue I was having.

Upvotes: 1

Views: 627

Answers (3)

Dinesh Shekhawat
Dinesh Shekhawat

Reputation: 539

Everything else is fine in your code.

Except for this line

int k = 1; //line39

this should be k = l (The letter 'L' in small caps)

You can refer the following code

public class StackExchange {
    public static

 void mergeSort(int A[], int l , int r) {

    if (l < r) {
        int m = (l+r)/2; 
        mergeSort(A, l , m);
        mergeSort(A, m+1, r);
        merge(A, l, m, r);
    }

}

private static void merge(int[] A, int l, int m, int r) {

    int n1 = m - l + 1;
    int n2 = r - m;

    int L[] = new int[n1];
    int R[] = new int[n2];

    for (int i = 0 ; i < n1; i++) {
        L[i] = A[l+i];
    }

    for (int j = 0 ; j < n2; j++) {
        R[j] = A[m + 1 + j];
    }

    int i = 0, j = 0 , k = l;

    while (i < n1 && j < n2) {
        if (L[i] <= R[j]) {
            A[k] = L[i];
            i++;
        } else {
            A[k] = R[j];
            j++;
        }

        k++;
    }

    while (i < n1) { 
        A[k] = L[i]; 
        i++; 
        k++; 
    }

    while (j < n2) { 
        A[k] = R[j]; 
        j++; 
        k++; 
    }
}

public static void main (String...s) {
    int array[] = new int[] {12, 21, 32, 36, 14, 10, 11, 5, 55, 16, 31, 7, 57, 89, 78};

    mergeSort(array, 0, array.length - 1);

    printArray(array);
}

private static void printArray(int array[]) {
    for (int i : array) {
        System.out.println(i + " -- ");
    }
}
}

Upvotes: 1

Akash Shah
Akash Shah

Reputation: 616

here example

 // Initial index of merged subarry array 
 int k = l; //this is L not a 1

Upvotes: 0

3rdeye7
3rdeye7

Reputation: 536

How are you calling your function ? Make sure that you are using object.sort(arr, 0, A.length-1); in main when you pass the max value of the array index.

Upvotes: 0

Related Questions