【问题标题】:Selection Sort in Java ArraylistJava Arraylist 中的选择排序
【发布时间】:2019-11-21 06:07:51
【问题描述】:

我需要帮助调试我的代码。它一直在犯错误,我不知道在哪里。这是我的选择排序方法:

private void selectionSort() {
        int best = 0;
        int j = 0;
        SortableRobot bestSoFar = botList.get(0);
        for(int i = 0;i<botList.size();i++) {
            int[] temp = botList.get(j).getLocation();
            for(int x = j;x<botList.size();x++) {
                if(botList.get(j).compareTo(botList.get(x)) < 0) {
                    // botList.get(j).moveToLocation(botList.get(x).getLocation());
                    // botList.get(x).moveToLocation(temp);
                    bestSoFar = botList.get(x);
                    best = x;
                }
            }
            SortableRobot tempbot = botList.get(j);
            botList.set(best,tempbot);
            botList.set(j, bestSoFar);
            j++;
        }
    }

【问题讨论】:

    标签: java sorting arraylist iteration


    【解决方案1】:

    问题是变量bestSoFar必须在每次迭代开始时设置。

    这个编辑使它在我的测试中工作:

    import java.util.ArrayList;
    import java.util.List;
    
    public class Test {
    
        private List<SortableRobot> botList;
    
        public static void main(String[] args) {
            new Test();
        }
    
        public Test() {
            botList = new ArrayList<SortableRobot>();
            botList.add(new SortableRobot(5));
            botList.add(new SortableRobot(3));
            botList.add(new SortableRobot(4));
            botList.add(new SortableRobot(1));
            botList.add(new SortableRobot(2));
    
            System.out.println("before sort: " + botList);
            selectionSort();
            System.out.println("after sort:  " + botList);
        }
    
        private void selectionSort() {
            int best = 0;
            int j = 0;
            SortableRobot bestSoFar = botList.get(0);
            for (int i = 0; i < botList.size(); i++) {
                bestSoFar = botList.get(j);// EDITED HERE the best bot so far has to be set to the current bot in the beginning of every iteration
                // int[] temp = botList.get(j).getLocation();
                for (int x = j; x < botList.size(); x++) {
                    if (botList.get(j).compareTo(botList.get(x)) < 0) {
                        // botList.get(j).moveToLocation(botList.get(x).getLocation());
                        // botList.get(x).moveToLocation(temp);
                        bestSoFar = botList.get(x);
                        best = x;
                    }
                }
                SortableRobot tempbot = botList.get(j);
                botList.set(best, tempbot);
                botList.set(j, bestSoFar);
                j++;
            }
        }
    
        private class SortableRobot implements Comparable<SortableRobot> {
    
            private int sortIndex;
    
            public SortableRobot(int sortIndex) {
                this.sortIndex = sortIndex;
            }
    
            public String toString() {
                return "SortableRobot[" + sortIndex + "]";
            }
    
            public int compareTo(SortableRobot o) {
                return Integer.compare(sortIndex, o.sortIndex);
            }
        }
    }
    

    输出是:

    before sort: [SortableRobot[5], SortableRobot[3], SortableRobot[4], SortableRobot[1], SortableRobot[2]]
    after sort:  [SortableRobot[5], SortableRobot[4], SortableRobot[3], SortableRobot[2], SortableRobot[1]]
    

    【讨论】:

      【解决方案2】:
      import java.util.ArrayList;
      import java.util.Arrays;
      import java.util.List;
      

      /* * 按索引选择 1 个元素并与直到最后一个索引的所有元素进行比较。如果下一个值大于比较值,则交换这两个值 * 并做同样的事情,直到我们得到最后一个索引。 */

      public class SortingSelection {
      
          public static void main(String[] args) {
              List<Integer> list = new ArrayList<>(Arrays.asList(40, 10, -30, 45, 39, 32));
      
              for (int i = 0; i < list.size(); i++) {
                  int selectionIndex = i;
      
                  for (int j = selectionIndex + 1; j < list.size(); j++) {
                      if (list.get(selectionIndex) > list.get(j)) {
                          int temp = list.get(selectionIndex);
                          list.set(selectionIndex, list.get(j));
                          list.set(j, temp);
      
                      }
                  }
                  System.out.println(list);
              }
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 2018-04-23
        • 2019-09-16
        • 2013-11-06
        • 1970-01-01
        • 2012-04-23
        • 2021-11-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多