I have an array of Faculty members and I want to use the Insertion Sort on it. I get an error at:
InsertionSort.insertionSortA(faculty, faculty.length);
The error says : "Method insertionSortA in class InsertionSort cannot be applied to given types;
required: Comparable [], int
found: Faculty [], int
I know that doing this will not work:
InsertionSort.insertionSortA((Comparable[]) faculty, faculty.length);
I know that if I had an Integer[] array would work, but I'm confused why my Faculty[] would not work?
public class Tester {
public static void main(String[] args) {
InsertionSort insertionSort = new InsertionSort();
Education edu = new Education ("BA", "Business", 1);
Education edu2 = new Education ("BA", "Health Science", 1);
Education edu3 = new Education ("BA", "Computer Science", 1);
Faculty[] faculty = new Faculty[] {
new Faculty ("538", "Doe", "Jane", 'M', 1994,1,10,
"Assistant", edu),
new Faculty ("238", "Do", "John", 'F', 1994,6,1,
"Assistant", edu2),
new Faculty ("080", "White", "Snow", 'F', 1994,4,22,
"Full", edu3)
};
InsertionSort.insertionSortA(faculty, faculty.length);
}
}
public class InsertionSort {
public static void insertionSortA(Comparable[] theArray, int n) {
for (int unsorted = 1; unsorted < n; ++unsorted) {
Comparable nextItem = theArray[unsorted];
int loc = unsorted;
while ((loc > 0) &&(theArray[loc-1].compareTo(nextItem) > 0)) {
theArray[loc] = theArray[loc-1];
loc--;
} // end while
theArray[loc] = nextItem;
} // end for
} // end insertionSort
public static void insertionSortB(Comparable[] theArray, int n) {
for (int unsorted = 1; unsorted < n; ++unsorted) {
Comparable nextItem = theArray[unsorted];
int loc = unsorted;
while ((loc > 0) &&(theArray[loc-1].compareTo(nextItem) < 0)) {
theArray[loc] = theArray[loc-1];
loc--;
} // end while
theArray[loc] = nextItem;
} // end for
}
}