Merge Sort code is not working and showing exception

前端 未结 3 1161
北海茫月
北海茫月 2021-01-17 06:59
public static void Merge(int[] arr,int p,int q,int r ) 
{
    int n1 = q-p;
    int n2 = r-q;
    int[] L=new int[n1];
    int[] R = new int[r-n2];

    for (int i =         


        
相关标签:
3条回答
  • 2021-01-17 07:24

    You get an Index out of bounds exception in this part:

    for (int i = 0; i < n2; i++)
    R[i] = arr[q+i];
    

    Your R-array is of size 0, while n2 is defined as 12 with the given arguments.

    0 讨论(0)
  • 2021-01-17 07:37

    Both you L and R array are defined as too small

    Initialize them like this instead:

    int[] L = new int[arr.Length];
    int[] R = new int[arr.Length];
    
    0 讨论(0)
  • 2021-01-17 07:44

    You array L is declared of size q-p, which are both 0

    0 讨论(0)
提交回复
热议问题