MergeSort error: 'Mege is undefined'

  • Thread starter Thread starter courtrigrad
  • Start date Start date
  • Tags Tags
    Program
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 1K views
courtrigrad
Messages
1,236
Reaction score
2
Hello all:

My program for MergeSort is not working. It keeps saying that Mege is undefined. Here is my code:

Code:
 public class MergeSort extends Merge {
public static void mergeSort(int[] a, int first, int last) {

        if(last==first) return;
        if (last - first == 1) {
            //  not a[0]>a[1]; this part could test on the upper half of the array
            if (a[first] > a[last]) {
                int temp = a[first];
                a[first] = a[last];
                a[last] = temp;
            }
            

        } else {
            int halfwayPoint = (int) (last + first) / 2;
            mergeSort(a, first, halfwayPoint);
            mergeSort(a, halfwayPoint + 1, last);
            merge(a, first, last, halfwayPoint);

        }
    }
}

How should I fix this problem? Any ideas are greatly appreciated.

Thanks :smile:
 
Physics news on Phys.org
courtrigrad said:
Hello all:

My program for MergeSort is not working. It keeps saying that Mege is undefined. Here is my code:

Code:
 public class MergeSort extends Merge {

How should I fix this problem? Any ideas are greatly appreciated.

Thanks :smile:

The "extends Merge" seems to be the problem. Are you trying to subclass another class Merge? If not, then just get rid of the "extends Merge".

You also need to define the function merge, which will merge together an array whose upper and lower halfs are sorted.
 
Last edited:
courtrigrad said:
Hello all:

My program for MergeSort is not working. It keeps saying that Mege is undefined. Here is my code:

Code:
 public class MergeSort extends Merge {
public static void mergeSort(int[] a, int first, int last) {

        if(last==first) return;
        if (last - first == 1) {
            //  not a[0]>a[1]; this part could test on the upper half of the array
            if (a[first] > a[last]) {
                int temp = a[first];
                a[first] = a[last];
                a[last] = temp;
            }
            

        } else {
            int halfwayPoint = (int) (last + first) / 2;
            mergeSort(a, first, halfwayPoint);
            mergeSort(a, halfwayPoint + 1, last);
            merge(a, first, last, halfwayPoint);

        }
    }
}

How should I fix this problem? Any ideas are greatly appreciated.

Thanks :smile:
It seems that you are trying to create a child class without having declared a parent class (if U know what I mean). You cannot write "extends Merge" without having declared a class called Merge. If that's not what you're trying to do (class hierarchy, I mean), just get rid of the "extends Merge" on the header. There's no point in creating a parent-child class relationship if the child does not need to inherit anything from the parent class.