I have written sorting methods for an array of comparables, insertion, merge, and selection, I've done this by altering code I had before from sorting an int array, and I just changed things from int to Comparable. However, When I was doing it for int arrays, I knew very well how to actually use the method, for example this is my my selection sort for ints:
public void selectionSort(int[] list){
for (int i=0;i<list.length;i++){
for (int si=i;si<list.length;si++){
if (list[si]<list[i]){
int temp=list[i];
list[i]=list[si];
list[si]=temp;
}
}
}
}
and this is the code which ends up using this method:
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int numItems,searchNum,location;
Sorts sort=new Sorts();
int[]test;
System.out.print("Enter the number of elements: ");
numItems=in.nextInt();
test=new int[numItems];
for (int i=0;i<test.length;i++){
test[i]=(int)(100*Math.random());
}
System.out.println("Unsorted: ");
displayArray(test);
sort.selectionSort(test);
System.out.println("Sorted: ");
displayArray(test);
and everything works fine, but for my comparable selection sort, I have this code:
public static void selectionSort(Comparable[] list){
for (int i=0;i<list.length;i++){
for (int si=i;si<list.length;si++){
if (list[si].compareTo(list[i])<0){
Comparable temp=list[i];
list[i]=list[si];
list[si]=temp;
}
}
}
}
but when I get to writing the code to test out this method, I just have no idea how to approach it, I don't know how I can make an array of Comparable interfaces, the concept is just so confusing for me and I can't find a way to make it work.