Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I am trying to write a merge sort algo. I can't tell if this is actually a canonical merge sort. If I knew how to calculate the runtime I would give that a go. Does anyone have any pointers? Thanks.

public static void main(String[] argsv) {

    int[] A = {2, 4, 5, 7, 1, 2, 3, 6};
    int[] L, R;

    L = new int[A.length/2];
    R = new int[A.length/2];

    int i = 0, j = 0, k;

    PrintArray(A);
    // Make left and right arrays
    for (k = 0; k < A.length; k++) {
        if (k < A.length/2) {
            L[i] = A[k];
            i++;
        }
        else {
            R[j] = A[k];
            j++;
        }
    }

    PrintArray(L);
    PrintArray(R);

    i = 0;
    j = 0;
    // Merge the left and right arrays
    for (k = 0; k < A.length; k++) {
        System.out.println(i + " " + j + " " + k);
        if (i < L.length && j < R.length) {
            if (L[i] < R[j]) {
                PrintArray(A);
                A[k] = L[i];
                i++;
            }
            else {
                PrintArray(A);
                A[k] = R[j];
                j++;
            }
        }
    }

    PrintArray(L);
    PrintArray(R);
    PrintArray(A);
}

public static void PrintArray(int[] arrayToPrint) {
    for (int i = 0; i < arrayToPrint.length; i++) {
        System.out.print(arrayToPrint[i] + " ");
    }
    System.out.print("\n");
}
share|improve this question
I asked here after reading Jeff Atwood's post here: blog.stackoverflow.com/2010/12/…;. "In a nutshell, Stack Overflow is for when you’re front of your compiler or editor working through code issues. Programmers is for when you’re in front of a whiteboard working through higher level conceptual programming issues. Hence the (awesome) whiteboard inspired design!" I guess I must have misinterpreted that. I am trying to learn mergesort for upcoming interviews – Beatrice Oct 16 '12 at 20:51
can you run the code? – Malachi Oct 16 '12 at 20:54
Hm. I guess that is what I am asking. Thanks for the advice. – Beatrice Oct 16 '12 at 20:54
@Malachi: yes, the code runs and correctly sorts an array with an even number of elements – Beatrice Oct 16 '12 at 20:55
I think what you are asking here is: what is the correct answer when asked in an interview: write a canonical Merge Sort. right? – Malachi Oct 16 '12 at 20:58
show 1 more comment

migrated from programmers.stackexchange.com Oct 16 '12 at 20:56

1 Answer

up vote 1 down vote accepted

when an prospective employer asks you a question like this one, they most likely want to see something original, they want you to show them that you can create code and not just copy and past bits and pieces of code.

if what you wrote works and runs correctly then you have accomplished the answer.

sometimes employers are trying to find out if you know the theory behind a merge sort which to me it looks like you have that as well.

share|improve this answer
1  
Thank you very much, I appreciate you taking the time to answer what I feel was an odd question. I suppose there must be a bunch of mergesort implementations, i.e. ones written in functional languages, ones that deal with an odd number of elements, ones that use sentinels, etc. Thanks again Malachi :) – Beatrice Oct 16 '12 at 21:03
you are welcome! – Malachi Oct 16 '12 at 21:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.