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;

}

Recommended Answers

All 2 Replies

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.

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.

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.