0

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.

2
  • 1
    The error says it all really. Change variable temp to be of type Player instead of int. Commented Nov 5, 2016 at 11:23
  • The outer loop must start from index 0, NOT 1, and must end at players.size() - 1, the inner loop must start from index i+1. en.wikipedia.org/wiki/Selection_sort#Implementation Commented Nov 5, 2016 at 11:27

2 Answers 2

1

A couple of things:

  • you are initializing an empty result array, but the algorithm works on existing arrays by swapping elements. You must initialize result with the values of players

    List<Player> result = new ArrayList<Player>(players);
    
  • variable temp must be of type Player instead of int

    Player temp = result.get(smallIntIndex);
    
  • the outer loop must start from index 0, not 1

  • the outer loop must end at players.size() - 1
  • the inner loop must start from index i+1
  • you swap elements every time in the outer loop, this is not correct. Only swap them when a new minimum is found

The corrected code:

public ArrayList<Player> ranking() {
    List<Player> result = new ArrayList<Player>(players);
    int smallInt = 0;
    int j=0;
    int smallIntIndex = 0;      

    for(int i=0;i<result.size() - 1;i++){
        smallInt = result.get(i).totalScore();
        smallIntIndex = i;
        for(j=i+1;j<result.size();j++){
            if(result.get(j).totalScore()<smallInt){
                smallInt = result.get(j).totalScore();
                smallIntIndex = j;                    
            }
        }

        if (i != smallIntIndex) {
            Player temp = result.get(smallIntIndex);
            result.set(smallIntIndex, result.get(i));
            result.set(i, temp);
        }
    }
    return result;
}

EDIT: You ask that the sorted results must go into a separate results array, which is initially empty. Here's a way to do it:

public ArrayList<Player> ranking() {
    List<Player> result = new ArrayList<Player>();
    int smallInt = 0;
    int j=0;
    int smallIntIndex = 0;      

    for(int i=0;i<players.size() - 1;i++){
        smallInt = players.get(i).totalScore();
        smallIntIndex = i;
        for(j=i+1;j<players.size();j++){
            if(players.get(j).totalScore()<smallInt){
                smallInt = players.get(j).totalScore();
                smallIntIndex = j;                    
            }
        }

        if (i != smallIntIndex) {
            Player player = players.get(smallIntIndex);

            result.add(player);
            players.set(smallIntIndex, players.get(i));
        }
    }
    return result;
}

Reference: Wikipedia's article on selection sort

Sign up to request clarification or add additional context in comments.

9 Comments

Hmm. result is just created when the method is called, so does that mean it holds the same values as the ArrayList players which are then sorted? Should values be inserted into result, or maybe lines like if(result.get(j).totalScore()<smallInt) shouldnt include result? Or am just wrong and result contains the same values as players?
Im afraid its not actually sorting them. its just returning the same values in the same order they were entered. But its not giving me a compilation error, so thats good.
Oh wait, how are you displaying them? Im using System.out.println(T.ranking()); and it outputs [] ? How to properly display it?
ArrayList<Player> winners2 = T.ranking(); for(int i = 0; i <winners2.size(); i++) System.out.println(T.ranking()); . Like that? Sorry, im not quite sure I understood what you meant
|
0

If you change

int temp = result.get(smallIntIndex).totalScore();

to

Player temp = result.get(smallIntIndex);

the program will compile.

The original code tries to insert Persons's totalScore into the list, instead of Person.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.