简体   繁体   中英

Why is this MergeSort not working?

I used the top-down psuedo-code from Wikipedia when creating this test code:

public static void main(String args[]) {
    int[] nums = new int[] { 17, 5, 3, 7, 6, 3, 11, 2 };
    mergeSort(nums);
    for (int i = 0; i < nums.length; i++) {
        System.out.print(nums[i] + " ,");
    }
}

public static void mergeSort(int[] A) {
    int[] B = new int[A.length];
    System.arraycopy(A, 0, B, 0, A.length);
    splitMerge(B, 0, A.length, A); // sort data from B[] into A[]
}

public static void splitMerge(int[] B, int begin, int end, int[] A) {
    if (end - begin < 2)
        return;
    int middle = (end + begin) / 2;
    splitMerge(B, begin, middle, A);
    splitMerge(B, middle, end, A);
    System.out.println("begin: " + begin +  " mid: " + ((end - begin)/2) + " end: " + end + " SIZE: " + (end-begin));
    merge(B, begin, middle, end, A);

}

public static void merge(int[] B, int begin, int middle, int end, int[] A) {
    int i = begin;
    int j = middle;
    for (int k = begin; k < end; k++) {
        if (i < middle && (j >= end || B[i] <= B[j])) {
            A[k] = B[i];
            i = i + 1;
        } else {
            A[k] = B[j];
            j = j + 1;
        }
    }
}

This should be more or less exactly the same as the psuedo-code, so why is it not working? Here is the output:

begin: 0 mid: 1 end: 2 SIZE: 2
begin: 2 mid: 1 end: 4 SIZE: 2
begin: 0 mid: 2 end: 4 SIZE: 4
begin: 4 mid: 1 end: 6 SIZE: 2
begin: 6 mid: 1 end: 8 SIZE: 2
begin: 4 mid: 2 end: 8 SIZE: 4
begin: 0 mid: 4 end: 8 SIZE: 8
6 ,3 ,11 ,2 ,17 ,5 ,3 ,7 ,

Your B array contains a copy of the original array and you never change it, while you change your original A array.

This means that the merge method cannot work, since it merges two unsorted arrays (two sub-arrays of B , which remains unsorted throughout the algorithm).

In order to do the merge properly, you should copy the elements having indices from begin to end from the A array to the B array at the start of the merge method.

All you have to do is add the statement

System.arraycopy(A, begin, B, begin, end - begin);

as the first statement of your merge method.

ie:

public static void merge(int[] B, int begin, int middle, int end, int[] A) {
    System.arraycopy(A, begin, B, begin, end - begin);
    int i = begin;
    int j = middle;
    for (int k = begin; k < end; k++) {
        if (i < middle && (j >= end || B[i] <= B[j])) {
            A[k] = B[i];
            i = i + 1;
        } else {
            A[k] = B[j];
            j = j + 1;
        }
    }
}

This will produce the output

2 ,3 ,3 ,5 ,6 ,7 ,11 ,17 ,

for the given input.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM