Question: How to write a generic selection sort method for ArrayLists in Java? I am doing an assignment where I could use the library function if
How to write a generic selection sort method for ArrayLists in Java? I am doing an assignment where I could use the library function if I want to sort the arraylists, but I am doing the bonus challenge of writing the algorithm myself. I want to use selection sort if possible. Here is the code:
public static> ArrayList selectionSort(ArrayList arrList) { AnyType currentMinVal; for(int i = 0; i < arrList.size()-1; i++) { currentMinVal = arrList.get(i); int minIndex = i; for(int j = i+1; j < arrList.size(); j++) { if(currentMinVal.compareTo(arrList.get(j))) { currentMinVal = arrList.get(j); minIndex = j; } } AnyType temp = arrList.get(minIndex); arrList.get(minIndex) = arrList.get(i); arrList.get(i) = temp; } return arrList; } Here is the problem:
I think I have an issue on the line:
if(currentMinVal.compareTo(arrList.get(j)))
It is saying: "incompatible types: int cannot be converted to boolean." Also: "unexpected type required: variable found: value" my main function is just me creating the array lists... I have one with Strings, one with Doubles, and another with Integers.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
