使用数组列表进行选择排序?

Selection sort with arraylists?

我必须为学校的事情调整选择排序。目的是 return 对玩家进行排名,获胜者在前,等等。排名相同的玩家应该按照他们在锦标赛列表中出现的顺序排列。

public ArrayList<Player> ranking() {
    ArrayList<Player> result = new ArrayList<Player>();
    // Supply this code!
    return result;

下面是我调整选择排序的尝试

    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;

唯一给我错误的是最后一行

result.set(i-1, temp); //The method set(int, Player) in the type 
                       //ArrayList<Player> is not applicable for the arguments (int, int)

知道我做错了什么吗?大部分适配是正确的吗?任何帮助表示赞赏。谢谢

p.s。请不要推荐比较器或类似的东西,那不是我要找的。

如果你改变

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

Player temp = result.get(smallIntIndex);

程序将编译。

原始代码尝试将 PersonstotalScore 插入列表,而不是 Person

几件事:

  • 您正在初始化一个空 result 数组,但该算法通过交换元素对现有数组起作用。您必须使用 players

    的值初始化 result
    List<Player> result = new ArrayList<Player>(players);
    
  • 变量 temp 的类型必须是 Player 而不是 int

    Player temp = result.get(smallIntIndex);
    
  • 外循环必须从索引0开始,不是1

  • 外层循环必须在 players.size() - 1
  • 处结束
  • 内部循环必须从索引i+1
  • 开始
  • 你每次都在外循环中交换元素,这是正确的。只有在找到新的最小值时才交换它们

更正后的代码:

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;
}

编辑: 您要求排序后的结果必须放入一个单独的 results 数组中,该数组最初是空的。这是一种方法:

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;
}

参考:Wikipedia's article on selection sort