I was recently writing a simple generic selection sort method for the fun of it, when I got a little confused about something. Here's my code:
public static <T extends Comparable<T>> void sort(List<T> list) {
for (int i = 0; i < list.size(); i++) {
int minIndex = i; // Assume that the first element is the minimum for now.
// Find smallest element in unsorted sub-list.
for (int k = i + 1; k < list.size(); k++) {
if (list.get(k).compareTo(list.get(minIndex)) < 0) {
minIndex = k;
}
}
// Swap smallest element with the first element in unsorted sub-list.
T temp = list.get(i);
list.set(i, list.get(minIndex));
list.set(minIndex, temp);
}
}
The function itself works fine, but I got a little confused about the generics. I use:
<T extends Comparable<T>>
to ensure that the given List has a comparable type. But what if I were to use a raw Comparable instead? That is:
<T extends Comparable>
What exactly would be the repercussions?
Thanks!