I've got this heap sort code, it was originally written to sort an array of integers but I need to sort an Object[] from a class. I keep getting this error for these lines:
public static void heapSort(Object[] myArray, int length) {
Object temp;
int size = length-1;
for (int i = (length / 2); i >= 0; i--) {
heapify(myArray, i, size);
};
for(int i= size; i>=0; i--) {
temp = myArray[0];
myArray[0] = myArray[size];
myArray[size] = temp;
size--;
heapify(myArray, 0, size);
}
}
public static void heapify (Object [] myArray, int i, int heapSize) {
int a = 2*i;
int b = 2*i+1;
int largestElement;
if (a<= heapSize && myArray[a] > myArray[i]) {
largestElement = a;
} else {
largestElement = i;
}
if (b <= heapSize && myArray[b] > myArray[largestElement]) {
largestElement = b;
}
if (largestElement != i) {
Object temp = myArray[i];
myArray[i] = myArray[largestElement];
myArray[largestElement] = temp;
heapify(myArray, largestElement, heapSize);
}
}
Sort.java:25: error: bad operand types for binary operator '>' if (a<= (heapSize && myArray[a] > myArray[i])) {
first type: Object second type: Object Sort.java:30: error: bad operand types for binary operator '>' if (b <= (heapSize && myArray[b] > myArray[largestElement])) {
first type: Object second type: Object
What does this error mean, I thought it was the bracket placements but it didn't fix the errors.
Is there a better sorting algorithm to sort through an object array created from a class.