ok. i have a standard mergesort class.
public class Merge extends SortAlgorithm {
public static void sort(Comparable[] a) {
Comparable[] aux = new Comparable[a.length];
sort(a, aux, 0, a.length -1);
}
protected static void sort(Comparable[] a, Comparable[] aux, int lo, int hi) {
if(hi <= lo)
return;
int mid = (lo + hi)/2;
sort(a, aux, lo, mid);
sort(a, aux, mid+1, hi);
if(less(a[mid+1], a[mid]))
merge(a, aux, lo, mid, hi);
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] a = br.readLine().split(" ");
sort(a);
show(a);
}
protected static void merge(Comparable[] a, Comparable[] aux, int lo, int mid, int hi) {
int i = lo , j = mid+1;
for(int k = lo ; k <= hi ; k++)
aux[k] = a[k];
for( int k = lo ; k <= hi ; k++ ) {
if(i > mid) //Boundary Conditions
a[k] = aux[j++]; //
else if(j > hi) //
a[k] = aux[i++]; //
else if(less(aux[i] , aux[j]))
a[k] = aux[i++];
else
a[k] = aux[j++];
}
}
}
I have extended this class to create a newer version of mergesort which uses less amount of auxiliary array. it has a different 'merge' and 'sort(Comparable[])' function
public class MergeSmallerAuxArray extends Merge {
public static void sort(Comparable[] a) {
Comparable[] aux = new Comparable[a.length/2];
sort(a, aux, 0, a.length - 1);
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] a = br.readLine().split(" ");
sort(a);
show(a);
}
//method overidden below
public static void merge(Comparable[] a, Comparable[] aux, int lo, int mid, int hi) { int i = lo , j = mid+1;
for(int k = lo ; k <= mid ; k++)
aux[k] = a[k];
for(int k = lo ; k <= hi ; k++) {
if(i > mid)
a[k] = a[j++];
else if(j > hi)
a[k] = aux[i++];
else if(less(aux[i], a[j]))
a[k] = aux[i++];
else
a[k] = a[j++];
}
}
}
but each time i run this class i am getting an error because the sort(Comparable[],Comparable[],int,int)
method of the superclass is calling the merge function of the superclass instead of the one which i had overridden. Where did i went wrong?
Please Help.
Thank you.