【问题标题】:Sorting an object array using Insert and Selection sorts使用插入和选择排序对对象数组进行排序
【发布时间】:2016-11-26 13:54:16
【问题描述】:

我需要创建一个方法来对对象数组进行排序。我从来没有这样做过,但我必须为我的课程修改它。在实现排序方法时,我完全迷失了。我需要使用插入排序选择排序进行排序。

这是我目前的代码。我所要做的就是在用户希望时调用 sort()。

package citylisttest;

public class CityList {
    private City[] city;
    private Integer numberOfCities;

    public CityList (Integer cityListSize){
        this.city=new City[cityListSize];
        this.numberOfCities=0;
    }

    public void addCity(String city){
        this.city[this.numberOfCities]=new City(city);
        this.numberOfCities++;
    }

    public String toString(){
        String cityDetails=new String();
        if (this.numberOfCities!=0){
            cityDetails+=String.format("%-15s\n","CITY");
            for(Integer i=0;i<this.numberOfCities;i++) {
                cityDetails+=this.city[i]+"\n"; }
        }
        else
            cityDetails+="City list is empty";
            return cityDetails;
        }

     public void sort(){

    }
}

【问题讨论】:

标签: java arrays sorting object insertion-sort


【解决方案1】:

首先,我建议将变量city 重命名为cities,因为它是一个数组,并且包含多个城市。此外,还可以考虑通过将实例变量标记为私有并分别创建 getter 和 setter 来封装数据。

假设你想按城市数量升序对它们进行排序,那么你的排序方法应该有:

for (int i = 0; i < city.length - 1; i++) {
    for (int j = i + 1; j < city.length; j++) {
        if (city[i].getNumberOfCities() > city[j].getNumberOfCities()) {
            City temp_city = city[i];
            city[i] = city[j];
            city[j] = temp_city;
        }
    }
}

我希望这会有所帮助,但您可以实现Comparable 接口或在this tutorial 之后创建Comparator 类。

编辑:如果要使用 compareto,按升序对城市名称进行排序:

for (int i = 0; i < city.length - 1; i++) {
    for (int j = i + 1; j < city.length; j++) {
        if (city[i].getName().compareTo(city[j].getName()) > 1) {
            City temp_city = city[i];
            city[i] = city[j];
            city[j] = temp_city;
        }
    }
}

假设 x 和 y 是字符串,x.compareTo(y) 给你:

如果 x > y,则为正数

如果 x 等于 y,则为零

如果 x 为负数

【讨论】:

  • 我了解如何做到这一点,它只是使用比较让我感到困惑。我需要按字母顺序排序,但感谢您的提示。
  • @Stefza,我已经编辑了我的答案,并且我还添加了关于compareTo的简要说明
  • @Stefza,虽然我知道您需要选择或插入排序,但我使用冒泡排序是为了让您找出正确的答案。如果您需要指南,可以查看此github.com/bit0001/SortAndSearchAlgorithms,其中包含用 Python 实现的几种排序算法。
  • 抱歉回复晚了。谢谢您的帮助!我已经看过各种笔记,但仍然无法进入我的脑海哈哈!估计需要时间。
【解决方案2】:

关于这个主题的文档在互联网上很常见,但是“让我为你搜索一下”。

我建议您了解您想要做什么。所以我建议你先看看什么是排序算法:

https://en.wikipedia.org/wiki/Sorting_algorithm

然后,尤其是插入排序算法:

https://en.wikipedia.org/wiki/Insertion_sort

或选择排序:

https://en.wikipedia.org/wiki/Selection_sort

有人可以在这里给你答案,但如果你不去解决这个问题,你就不会了解它,你很快就会忘记它。

希望对你有帮助:)

【讨论】:

    【解决方案3】:

    这看起来很像一个家庭作业问题,因为通常的做法是不创建自己的排序算法。

    您可以尝试设计自己的解决方案,即使它很幼稚,也比复制/粘贴您在此处找到的任何答案更进一步。

    如果你真的想探索各种可能的解决方案(使用 java 源代码),你可以按照这个小程序和教程:

    https://thomas.baudel.name/Visualisation/VisuTri/

    【讨论】:

      【解决方案4】:

      这里是代码。但在去那里之前,我认为您应该观看以下两个视频:

      插入排序:https://www.youtube.com/watch?v=DFG-XuyPYUQ&t=142s

      选择排序:https://www.youtube.com/watch?v=f8hXR_Hvybo

      public static void insertionSort(Object[] data) {
      // i denotes where the partition is
      for (int i = 1; i < data.length; i++) {
      // the key is to the right of the partition
      Object key = data[i];
      int j = i - 1; // use j to scan left to insert key
      while (j >= 0 && ((Comparable) key).compareTo(data[j]) < 0) {
      // shift item right to make room
      data[j + 1] = data[j];
      j--;
      }
      // Found the position where key can be inserted
      data[j + 1] = key;
      }
      }
      
      public static void selectionSort(Object[] data) {
      for (int i = 0; i < data.length - 1; i++) {
      // Find the index of the minimum item, starting at `i'.
      int minIndex = i;
      for (int j = i + 1; j < data.length; j++) {
      if (((Comparable) data[j]).compareTo(data[minIndex]) < 0)
      minIndex = j;
      // Exchange with the first item (at `i'), but only if different
      if (i != minIndex) {
      Object tmp = data[i];
      data[i] = data[minIndex];
      data[minIndex] = tmp;
      }
      }
      }
      

      【讨论】:

        猜你喜欢
        • 2022-01-03
        • 2021-11-02
        • 2016-05-05
        • 1970-01-01
        • 1970-01-01
        • 2023-03-10
        • 1970-01-01
        相关资源
        最近更新 更多