Solved
Sorting
Posted on 2003-12-01
Hello,
I'm trying to test the insertionSort, quickSort, and mergeSort but I'm getting compilation errors on parts of my program. Below are the two methods (I think...) that are giving the errors:
-----------------------------------------------------------
private static void choosePivot(Comparable[] theArray, int first, int last) {
int pivotIndex;
int mid = (first + last)/2;
if (theArray[first] <= theArray[mid]) {
if (theArray[mid] <= theArray[last]) {
pivotIndex = mid;
}
else {
pivotIndex = (theArray[first] <= theArray[last] ? last:first);
}
}
else if(theArray[last] <= theArray[mid]) {
pivotIndex = mid;
}
else {
pivotIndex = (theArray[first] <= theArray[last] ? first:last);
swap(theArray[first], theArray[pivotIndex]);
}
} // end choosePivot
//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//
public void swap(int left, int right) {
Comparable temp = theArray[left];
theArray[left] = theArray[right];
theArray[right] = temp;
} // end swap()
-----------------------------------------------------
Can someone also explain what 'Comparable' is and what it does... because I get the following compilation errors:
operator <= cannot be applied to java.lang.Comparable,java.lang.Comparable
- and -
swap(int,int) cannot be applied to (java.lang.Comparable,java.lang.Comparable)
----------------------------------------------------
Also, the compiler's complaing that it can't find 'theArray' in my swap method:
cannot resolve symbol: Comparable temp = theArray[left];
^
----------------------------------------------------
If you need the rest of my program, please let me know. Thanks :)