0

I am new to Java and I have tried writing a Merge Sort Program . MergeSort.java program is below :-

public class MergeSort {

    public static int b[] = {6,2,3,1,9};

    public void MergeSort(int a[],int left[], int right[]) {

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

        int l = left.length;

        int r = right.length;
        int v = a.length;

        while ( i < l && j < r) {
            if( left[i] < right[j]) {
                a[k] = left[i] ;
                i++;
            } else {
                a[k] = right [j];
                j++;
            }
            k++;
        }
        while ( i < l ) {
            a[k] = left[i];
            k++;
            i++;

        }
        while ( j < r ) {
            a[k] = right[j];
            k++;
            j++;

        }

    }

    public void Merge(int a[], int length) {


        int n = length;
        if(n < 2) return;
        int mid = n/2;

        int left[] = new int[mid];
        int right[] = new int[n-mid];
        for (int i = 0 ; i <mid ; i++) {
            left[i] = a[i];
        }
        for (int i = mid ; i <n ; i++) {
            right[i-mid] = a[i];
        }

        Merge(right,n-mid);
        Merge(left,mid);
        MergeSort(b, left, right);

    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub


        MergeSort ms = new MergeSort();
        ms.Merge(b,b.length);
        for(int i=0 ; i < b.length ; i++)
        System.out.println(b[i]);

    }

}

When I run this program , the output I get is

3
1
6
2
9

But , the expected output is 1 2 3 6 9

Can anyone please help me point out the mistake I am doing and try to help me fix it please .I have tried debugging it on Eclipse but could not spot the bug . Thanks

Newbie
  • 2,664
  • 7
  • 34
  • 75
  • Why `Merge Sort` taking three `Array`s as formal arguments? One just needs a single `Array` which is to be sorted, and two `int` values to track of `low` and `high` ends of that `Array`. `Merge Sort` creates a temporary Array in the `Merge Sort` method, and after sorting the elements out and then copies the content of this temporary `Array` into the original `Array`, unlike `Quick Sort`, which does that in place. – nIcE cOw Sep 14 '14 at 04:53

1 Answers1

3

Are you sure you meant this in function Merge:

MergeSort(b, left, right);

It seems it should be:

MergeSort(a, left, right);
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
  • I had to pass the already split array instead of the original array . – Newbie Sep 14 '14 at 04:52
  • @Newbie I recommend you to use better names for your variables. – Luiggi Mendoza Sep 14 '14 at 04:53
  • Sure would consider this advice for my next code :) – Newbie Sep 14 '14 at 04:53
  • @Newbie Also try to avoid giving class name as a name for a method to avoid confusion and code readability. In your case "MergeSort" – rdp Sep 14 '14 at 04:54
  • @Newbie following `@dilip` suggestions, the names of your methods should be `split` and `merge`. Also check the lowercase. – Luiggi Mendoza Sep 14 '14 at 04:55
  • @Newbie : Adding further more, Arrays are declared as `int[] values` and not `int values[]`, do look how you did that in the `main` method, though never used that convention after that. – nIcE cOw Sep 14 '14 at 04:58