如何修复合并排序方法的 ArrayIndexOutOfBoundsException?

所以我遇到的问题是我试图让我的合并排序实现运行,但我不断收到一个异常错误,指出数组索引超出范围。这是一个运行时错误,因为我能够毫无问题地编译程序,并且它会一直运行,直到它遇到我的合并排序调用。我尝试的一件事是在合并方法中更改我的一个变量以匹配另一个变量(int k = 0;//第 39 行)。当我这样做时,代码运行了,但是,合并排序的数组不正确。我什至尝试调试代码,但看不到它的问题。下面是我的代码:


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++;

   }

}

这是错误:


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

我知道这意味着数组超出了 15 的设置大小,但我不确定如何解决这个问题。我曾尝试查看类似的问题,但我没有找到解决我遇到的问题的方法。


慕少森
浏览 226回答 3
3回答

长风秋雁

您的代码中的其他一切都很好。除了这条线诠释 k = 1; //第39行这应该是 k = l(小型大写字母中的字母“L”)您可以参考以下代码public class StackExchange {&nbsp; &nbsp; public static&nbsp;void mergeSort(int A[], int l , int r) {&nbsp; &nbsp; if (l < r) {&nbsp; &nbsp; &nbsp; &nbsp; int m = (l+r)/2;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; mergeSort(A, l , m);&nbsp; &nbsp; &nbsp; &nbsp; mergeSort(A, m+1, r);&nbsp; &nbsp; &nbsp; &nbsp; merge(A, l, m, r);&nbsp; &nbsp; }}private static void merge(int[] A, int l, int m, int r) {&nbsp; &nbsp; int n1 = m - l + 1;&nbsp; &nbsp; int n2 = r - m;&nbsp; &nbsp; int L[] = new int[n1];&nbsp; &nbsp; int R[] = new int[n2];&nbsp; &nbsp; for (int i = 0 ; i < n1; i++) {&nbsp; &nbsp; &nbsp; &nbsp; L[i] = A[l+i];&nbsp; &nbsp; }&nbsp; &nbsp; for (int j = 0 ; j < n2; j++) {&nbsp; &nbsp; &nbsp; &nbsp; R[j] = A[m + 1 + j];&nbsp; &nbsp; }&nbsp; &nbsp; int i = 0, j = 0 , k = l;&nbsp; &nbsp; while (i < n1 && j < n2) {&nbsp; &nbsp; &nbsp; &nbsp; if (L[i] <= R[j]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; A[k] = L[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i++;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; A[k] = R[j];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; k++;&nbsp; &nbsp; }&nbsp; &nbsp; while (i < n1) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; A[k] = L[i];&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; i++;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; k++;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; while (j < n2) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; A[k] = R[j];&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; j++;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; k++;&nbsp;&nbsp; &nbsp; }}public static void main (String...s) {&nbsp; &nbsp; int array[] = new int[] {12, 21, 32, 36, 14, 10, 11, 5, 55, 16, 31, 7, 57, 89, 78};&nbsp; &nbsp; mergeSort(array, 0, array.length - 1);&nbsp; &nbsp; printArray(array);}private static void printArray(int array[]) {&nbsp; &nbsp; for (int i : array) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(i + " -- ");&nbsp; &nbsp; }}}

慕婉清6462132

你是如何调用你的函数的?object.sort(arr, 0, A.length-1);传递数组索引的最大值时,请确保在 main 中使用。

四季花海

&nbsp;// Initial index of merged subarry array&nbsp;&nbsp;int k = l; //this is L not a 1
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java