I have to adapt selection sort for a school thing. The purpose is to return ranking of players with winners coming first, etc. Equally ranked players should be listed in the same order as they occur in the tournament list.
public ArrayList<Player> ranking() {
ArrayList<Player> result = new ArrayList<Player>();
// Supply this code!
return result;
Now heres my attempt at adapting selection sort
int smallInt = 0;
int j=0;
int smallIntIndex = 0;
for(int i=1;i<players.size();i++){
smallInt = result.get(i-1).totalScore();
smallIntIndex = i-1;
for(j=i;j<players.size();j++){
if(result.get(j).totalScore()<smallInt){
smallInt = result.get(j).totalScore();
smallIntIndex = j;
}
}
int temp = result.get(smallIntIndex).totalScore();
result.set(smallIntIndex, result.get(i-1));
result.set(i-1, temp);
}
return result;
And the only line that gives me error is the very last
result.set(i-1, temp); //The method set(int, Player) in the type
//ArrayList<Player> is not applicable for the arguments (int, int)
Any idea what I'm doing wrong? Is most of the adaptation correct? ANY HELP APPRECIATED. Thanks
p.s. please don't suggest comparators or stuff like that, that's not what I'm looking for.
tempto be of typePlayerinstead ofint.0, NOT1, and must end atplayers.size() - 1, the inner loop must start from indexi+1. en.wikipedia.org/wiki/Selection_sort#Implementation