简体   繁体   中英

Trouble with A Java Merge Sort Algorithm

I am having trouble with this merge sort algorithm. I have 3 total methods to do a merge sort, plus the main method calling it. It is not outputting a sorted array and I am not sure where I went wrong. It all looks right to me when I am looking at it, I am not sure if the error is within the recursive parts of the method, or if it's within just one of the classes. Any help would be appreciated, thanks: Here is my code:

/**
 * To do: Implement merge sort.
 * @param array the array to sort
 * @return the sorted array
 */
public static int[] mergeSort(int[] array) {
    // call mergeSort(int [], int, int) to initiate sorting
    //throw new UnsupportedOperationException()
    return mergeSort(array, 0, array.length-1);
}

/**
 * To do: Implement overloaded merge sort method.
 * @param array the array to sort
 * @param begin the starting index of the array to be sorted
 * @param end the last index of the array to be sorted
 */
private static int[] mergeSort(int[] array, int begin, int end) {
    // you need to write the merge sort algorithm here
    // use this method mergeSort(int [], int, int) and merge(int [], int[])
    // to complete it
    //throw new UnsupportedOperationException();


    if(begin < end) {
        int [] left = new int[array.length/2];
        int [] right = new int[array.length - left.length];

        //copies first half of array into left
        for(int i = 0; i < left.length; i++) {
            left[i] = array[i];
        }
        //copies second half into right array
        for(int j = 0; j < right.length; j++) {
            right[j] = array[left.length + j];
        }

        mergeSort(left);
        mergeSort(right);
        return merge(left, right);
    }
    return array;
}


/**
 * To do: Merge two sorted arrays into one and return it
 * @param left the first array
 * @param right the second array
 * @return the sorted merged array
 */
private static int[] merge(int[] left, int[] right) {
    // merge two sorted array such way that it remains sorted
    //throw new UnsupportedOperationException();
    int [] sorted = new int[left.length + right.length];

    int leftIndex = 0;
    int rightIndex = 0;

    for(int j = 0; j < sorted.length; j++ ) {
        if( leftIndex <= left.length-1 && rightIndex <= right.length-1) {

            if(left[leftIndex] < right[rightIndex]) {
                sorted[j] = left[leftIndex];
                leftIndex++;
            }
            else {
                sorted[j] = right[rightIndex];
                rightIndex++;
            }
        }
        else if( leftIndex < left.length) {
            sorted[j] = left[leftIndex];
            leftIndex++;
        }
        else if(rightIndex< right.length) {
            sorted[j] = right[rightIndex];
            rightIndex++;
        }
    }

    return sorted;
}

Hi I think we're in the same class. You have to use the "begin" and "end" values to create a sub-array from the original array parameter. After that you can separate it into "right" and "left", similar to what you have done already.

As for the the merge() method I would recommend looking at the code in the textbook. The only difference between the code in the book and our assignment is that we have to create a result array inside the method:

int[] array = new int[left.length-right.length];

and then return it when it's done. Hope that helped.

Your mergeSort function is not in-place, ie it returns a new array instead of changing the order of elements in the same array. Therefore, these 2 lines:

            mergeSort(left);
            mergeSort(right);

will have absolutely no effects.

Change it to:

            left = mergeSort(left);
            right = mergeSort(right);

Another comment: Even though your merge sort is O(NlogN) , you use a lot copying arrays around, which creates a lot of the overhead. Try to use in place operation as much as possible, avoid creating extra array. Most often, the merge sort uses no more than 2 arrays at any time.

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