Hello Friends.. need a help with Merge Sort

I tried to implement Merge Sort using Java language..I followed Introduction to Algorithm book for the algorithm.. but the code generates errors.. please can anyone explain this to me.. why these errors pop-up ??

package test;
import java.util.Random;
public class mergeSort {

    public static void main(String[] args) {

       int[] A=new int[20];
       Random generator = new Random();

       for(int c=0;c<20;c++){

           A[c]= generator.nextInt(100);
           System.out.print( A[c]+ "     ");
       }

       MyMergeSort sort = new MyMergeSort();

       sort.mergeSort(A, 0, 20);
       System.out.print( "\n");

       for(int c=0;c<20;c++){

           System.out.print( +A[c]+ "     ");
       }

 }

}

class MyMergeSort{

    void mergeSort(int A[],int p,int r){
        int q;
         if(p<r){
            q= (int) Math.floor((p+r)/2);
          try{
              mergeSort(A, p, q);
          }
          catch(Exception e){
              System.out.print(e.getMessage()+" one");
          }

             try{
              mergeSort(A, q+1, r);
          }
          catch(Exception e){
              System.out.print(e.getMessage()+" two");
          }

              try{
                 merge(A, p, q, r);
          }
          catch(Exception e){
              System.out.print("\n\n\n"+e.getMessage()+" three");
          }
         

            }
        }

    void merge(int A[],int p,int q,int r){
        int n1,n2;
        int[] L=new int[r];
        int[] R=new int[r];

        n1 = q-p+1;
        n2 = r-q;
        int i,j;
        for(i=1;i<n1;i++){
            L[i]=A[p+i-1];

        }
        for(j=1;i<n2;i++){

            R[j]=A[q+j];
        }

        L[n1+1]=(int)Double.POSITIVE_INFINITY;
        R[n2+1]=(int)Double.POSITIVE_INFINITY;

        i=1;
        j=1;

        for(int k=p;k<p;k++){

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


    }
}

thank you in advance..

Recommended Answers

All 10 Replies

the code generates errors

You forgot to post the errors.
Please copy and paste here the full text of the error messages.

You forgot to post the errors.
Please copy and paste here the full text of the error messages.

ok.here's the output.. Line 4 and 7 are the output of the Exception catch block..

62   78   49   3   40   19   13   0   22   20   31   99   52   15   46   66   87   78   11   1   


2 three


3 three
62   78   49   3   40   19   13   0   22   20   31   99   52   15   46   66   87   78   11   1

You still didn't post the errors.

These Errors pop-up when I comment the try-catch blocks.. unless there's no such errors.. only a msg showing that there are errors.. Btw I'm using Netbeans.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
61 1 27 66 56 63 92 0 54 73 82 63 78 34 24 36 90 82 96 32 at test.MyMergeSort.merge(mergeSort.java:83)
at test.MyMergeSort.mergeSort(mergeSort.java:56)
at test.MyMergeSort.mergeSort(mergeSort.java:42)
at test.MyMergeSort.mergeSort(mergeSort.java:42)
at test.MyMergeSort.mergeSort(mergeSort.java:42)
at test.MyMergeSort.mergeSort(mergeSort.java:42)
at test.mergeSort.main(mergeSort.java:21)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

The error clearly states that on line 83 of your mertSort class you are trying to access index 2 of an array that does not have that many elements.

Find out why your indices are going out of bounds. Use println statements to test the conditions.

The error clearly states that on line 83 of your mertSort class you are trying to access index 2 of an array that does not have that many elements.

Find out why your indices are going out of bounds. Use println statements to test the conditions.

ok I'll try and let you know.. thanks

The error clearly states that on line 83 of your mertSort class you are trying to access index 2 of an array that does not have that many elements.

Find out why your indices are going out of bounds. Use println statements to test the conditions.

I commented Line 83 and 84 (corresponding to above code lines 79 and 80).. but still it shows program running with some errors..but not showing anything (any errors).

it shows program running with some errors

Please explain. How do you know there are "some errors"?
Do you mean the output is not what you want?

For simpler testing and posting of results, use the shortest list of elements to sort that will show the problem.

Please explain. How do you know there are "some errors"?
Do you mean the output is not what you want?

For simpler testing and posting of results, use the shortest list of elements to sort that will show the problem.

When I click on the "Run File" command in netbeans it shows there are some errors.. and when I clicked on "Run anyway". then the above output shows..you can see the array is not sorted..

Firstly, "run anyway" is a waste of time - if there are compiler errors then you need to fix those before attempting to run anything.
Secondly, if you comment out lines of code from your algorithm then of course it won't work.
Thirdly, make sure every catch block has an e.printStackTrace(); so you can see all the details of any errors during execution.

Step one is get a clean compile of all the code.
If you need help with compile errors post the exact code that you compiled and the exact compiler error message (including line number(s)). Saying "some errors" gives us absolutley nothing to go on.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.