hi everyone im studing computer science , i have question about margeSorting i do not undrstant how it going ???

i do not know where i shold put my question

public static int[] mergeSort(int[] a) {
    if (a.length == 1) {
        return a;
    } else {
        int n = a.length / 2;
        int b[] = new int[n];
        int c[] = new int[a.length - n];
        int i;

        for (i = 0; i < a.length; i++) {
            if (i < n) {
                b[i] = a[i];
            } else {
                c[i - n] = a[i];
            }
        }

        b = mergeSort(b);
        c = mergeSort(c);
        a = merge(b, c);

        return a;
    }
}

public static int[] merge(int[] A, int[] B) {

    int c[] = new int[A.length + B.length];
    int i, a = 0, b = 0;

    for (i = 0; i < A.length + B.length; i++) {

        if (a == A.length) {
            c[i] = B[b];
            ++b;
        } else if (b == B.length) {
            c[i] = A[a];
            ++a;
        } else if (A[a] > B[b]) {
            c[i] = B[b];
            ++b;
        } else {
            c[i] = A[a];
            ++a;
        }

    }

    return c;

}

That's mergesort, not margesort. That's probably why you had trouble finding it on Google.
Anyway, Google "mergesort" and you'll get a huge range of tutorials that explain it.

This one seems very clear and not too technical, so it's a good place to start.

Edited 2 Years Ago by JamesCherrill

also: saying that you "have a question about it" will not help you find an answer just as fast as stating what the actual question is.

This article has been dead for over six months. Start a new discussion instead.