0

I am trying to implement Merge Sort algorithm on an array of genetic type. The problem is that the result is incorrect since some elements were set to 0. I suspect that the problem came from the casting I did through out the program, but I do not know exactly why.

As you can see in sortUsingMerge(), I casted an array of Object to generic. Also in the IF statement below, I casted the Object element in the array to int to be able to compare 2 elements. My intention is that the sorting algorithm should work for any array of any type, but I just do not want to write the Comparator for each type for now.

Input array: 5 1 -2 3 7 8 0
Output: -2 0 1 1 3 7 0

Look like number 5 and 8 were somehow converted to different numbers during the sorting. Can someone show me why? Thanks!

Source code:

public void sortUsingMerge(){
    Object[] array = (E[]) new Object[size];
    Object[] helper = (E[]) new Object[size];

    mergeSort(array,helper,0,size-1);

}
 public void mergeSort(Object[] array, Object[] helper, int low, int high){

       if (low<high){ 

            int mid = (low + high)/2;

            mergeSort(array,helper,low,mid); //sort left half
            mergeSort(array,helper,mid+1,high);//sort right half
            merge(array,helper,low,mid,high);

        }
}
public void merge(Object[] array, Object[] helper, int low, int mid, int high){

    for (int i=low; i<=high; i++){// copy both parts into helper array
        helper[i] = array[i];
    }

    int helperLeft = low;
    int helperRight = mid + 1;
    int current = low;

    while (helperLeft <= mid && helperRight <=high){

        if ((int)helper[helperLeft] <= (int)helper[helperRight]){
            array[current] = helper[helperLeft];
            helperLeft++;
        }
        else{
            array[current] = helper[helperRight];
            helperRight++;
        }

        current++;
    }


    int remain = mid - helperLeft;
    for (int i=0; i<remain; i++){
        array[current+i] = helper[helperLeft + i];
    }


}
4
  • 3
    use the debugger. Write some tests. Commented Nov 23, 2013 at 7:07
  • 1
    It would appear you're losing numbers because you didn't make a "deep" copy for your left and right arrays.. you merely modify in place. Do as mitch suggested and debug .. Commented Nov 23, 2013 at 7:13
  • 1
    if you are casting your array element to int in your logic, why use generics? Commented Nov 23, 2013 at 7:50
  • My intention was to make the sorting work for any type of element. However I wanted to quickly check to see if it worked with integers first. Without the cast, I cannot compare an E to another E. Commented Nov 23, 2013 at 16:48

2 Answers 2

4

inside merge replace

    for (int i=0; i<remain; i++){
     array[current+i] = helper[helperLeft + i];
    }

with

    while (helperLeft <= mid) {
        array[current] = helper[helperLeft];
        current++;
        helperLeft++;
    }
Sign up to request clarification or add additional context in comments.

3 Comments

Link-only answers are discouraged here, for multiple reasons. Please put an actual, concise answer to the specific problem here.
@bdares concise answer has been placed.
It fixed the problem. I did not expect problem came from the algorithm itself :) Thanks!
1

Casting would never give you that kind of error. You have bug at the end of your merge.

    int remain = mid - helperLeft;
    for (int i=0; i <= remain; i++) {                 //changed < to <=
        array[current+i] = helper[helperLeft + i];
    }

1 Comment

I believe the 2nd for loop is not necessary since the biggest elements are always moved to right end of the original array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.