Troubleshooting MergeSort: Fixing the 'Mege is undefined' Error

  • Thread starter Thread starter courtrigrad
  • Start date Start date
  • Tags Tags
    Program
Click For Summary
SUMMARY

The forum discussion addresses a coding issue with a MergeSort implementation in Java, specifically the error message indicating that 'Mege is undefined'. The problem arises from the incorrect use of the 'extends Merge' clause without a defined parent class. Participants recommend removing the 'extends Merge' if subclassing is unnecessary and emphasize the need to implement the 'merge' function to combine sorted halves of the array.

PREREQUISITES
  • Java programming language fundamentals
  • Understanding of MergeSort algorithm
  • Knowledge of class inheritance in Java
  • Ability to implement sorting algorithms
NEXT STEPS
  • Implement the 'merge' function for the MergeSort class
  • Review Java class inheritance and polymorphism concepts
  • Study common sorting algorithms and their implementations
  • Debug Java code using an IDE like IntelliJ IDEA or Eclipse
USEFUL FOR

Java developers, computer science students, and anyone interested in understanding sorting algorithms and debugging Java code.

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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
Replies
4
Views
10K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 18 ·
Replies
18
Views
3K