使用arraylists进行选择排序?

时间:2016-11-05 11:19:36

标签: java arraylist selection-sort

我必须调整学校用品的选择排序。目的是返回获胜者排名第一的球员排名等。同等排名的球员应按照比赛名单中的顺序排列。

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。请不要建议比较器或类似的东西,这不是我想要的。

2 个答案:

答案 0 :(得分:1)

有几件事:

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

    的值初始化players
    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

答案 1 :(得分:0)

如果你改变了

  

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

  

玩家temp = result.get(smallIntIndex);

程序将编译。

原始代码尝试将Persons的{​​{1}}插入到列表中,而不是totalScore