Troubleshooting MergeSort: Fixing the 'Mege is undefined' Error

  • Thread starter Thread starter courtrigrad
  • Start date Start date
  • Tags Tags
    Program
AI Thread Summary
The issue with the MergeSort program stems from the use of "extends Merge" without a defined parent class, leading to the 'Mege is undefined' error. To resolve this, either declare the Merge class if subclassing is intended or remove "extends Merge" if not needed. Additionally, the merge function, which is essential for combining sorted halves of the array, must be defined to complete the MergeSort implementation. Properly addressing these points will help fix the program's functionality. Ensuring the class structure and method definitions are correct is crucial for successful execution.
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.
 
Thread 'Variable mass system : water sprayed into a moving container'
Starting with the mass considerations #m(t)# is mass of water #M_{c}# mass of container and #M(t)# mass of total system $$M(t) = M_{C} + m(t)$$ $$\Rightarrow \frac{dM(t)}{dt} = \frac{dm(t)}{dt}$$ $$P_i = Mv + u \, dm$$ $$P_f = (M + dm)(v + dv)$$ $$\Delta P = M \, dv + (v - u) \, dm$$ $$F = \frac{dP}{dt} = M \frac{dv}{dt} + (v - u) \frac{dm}{dt}$$ $$F = u \frac{dm}{dt} = \rho A u^2$$ from conservation of momentum , the cannon recoils with the same force which it applies. $$\quad \frac{dm}{dt}...
Back
Top