【问题标题】:Selection Sort Java选择排序 Java
【发布时间】:2013-10-22 02:45:29
【问题描述】:

我正在尝试根据 Coin 对象的值按降序对一组 Coin 对象进行排序。我的 Coin 类中有一个 getValue() 方法。我的问题是最终结果根本没有排序。这就是我最终得到的。我似乎无法弄清楚我哪里出错了,任何提示都会有所帮助

在我们排序之前: [Coin[value=0.25,name=quarter], Coin[value=0.01,name=penny], Coin[value=0.1,name=dime], Coin[value=1.0,name=dollar], Coin[value=0.05 ,name=nickel]]

预期:[Coin[value=0.25,name=quarter], Coin[value=0.01,name=penny], Coin[value=0.1,name=dime], Coin[value=1.0,name=dollar],硬币[价值=0.05,名称=镍]]

排序后: [Coin[value=0.01,name=penny], Coin[value=0.1,name=dime], Coin[value=0.25,name=quarter], Coin[value=1.0,name=dollar], Coin[value=0.05 ,name=nickel]]

预期:[Coin[value=1.0,name=dollar], Coin[value=0.25,name=quarter], Coin[value=0.1,name=dime], Coin[value=0.05,na​​me=nickel],硬币[value=0.01,name=penny]]

import java.util.Arrays;

/**
   This class sorts an array of coins, using the selection sort
   algorithm.
*/
public class CoinSelectionSorter
{
   //
    private Coin[] list;

   /**
      Constructs a selection sorter.
      @param anArray the array to sort.
   */
   public CoinSelectionSorter(Coin[] anArray)
   {
      list = anArray;
   }

    public String toString()
   {
      return Arrays.toString(list);
   }
   /**
      Finds the largest coin in an array range.
      @param from the first position in a to compare
      @return the position of the largest coin in the
      range a[from] . . . a[a.length - 1]
   */
   public int maximumPosition(int from)
   {
      int max = from;
      for(int i = 0; i < list.length-1; i++){
          if(list[i].getValue() > list[max].getValue()){
              max = i;
          }  
      }
      return max;
   }

   /**
      Sorts an array.
   */
   public void sort()
   {
      for(int i = 0; i < list.length -1; i++){
          int max = maximumPosition(i);
          swap(i, max);
      }
   }

   /**
      Swaps two entries of the array.
      @param i the first position to swap
      @param j the second position to swap
   */
   public void swap(int i, int j)
   {
      Coin temp = list[i];
      list[i] = list[j];
      list[j] = temp;
   }
}

【问题讨论】:

  • 我不知道这是否是你的问题,但我认为你写i &lt; list.length-1的所有地方都应该是i &lt; list.length
  • 另外,在同一行,int i = from + 1 而不是int i = 0
  • 看看你的 maximumPosition() 函数。在您的 cmets 中,您说该函数应该“返回 a[from] ... a[a.length - 1] 范围内最大硬币的位置”。但是,它不是这样做的。
  • 是的,问题是使用 0 而不是 from 和使用 i

标签: java arrays selection-sort


【解决方案1】:

你可以在List中引入数组,在对列表进行排序后,最后再把这个参数引入数组,比使用这些更容易

在一个Coin类中,引入公共类Coin实现Comparable

你应该实现一个方法:CompareTo(Coin p) // p 是一个例子

在这个方法中引入:return this.r-prueva.r; // 你在接下来的步骤中使用这个 Comparable.Sort 在接下来的步骤中

示例:

public class Coin implements Comparable<Coin >{

    Integer r;
    String p;

    public Coin(Integer r,String p) {
        // TODO Auto-generated constructor stub
        this.r = r;
        this.p = p;
    }

    @Override
    public int compareTo(Coin test) {
        // TODO Auto-generated method stub
        return this.r - test.r;
    }
}

没关系...现在在您的班级中,您可以创建一个方法或引入此方法来对数组进行排序:

List<Coin> fileList = Arrays.asList(list); // Introduce Array in List

Collections.sort(list); // sort List

list = filelist.toArray(list) // introduce the sorted list in array

非常简单...如果您愿意,如果您不了解我的语言,我可以显示此代码来满足您的需求:

您使用可比较实现的硬币类可能是这样......

  public class Coin implements Comparable<Coin >{

        Integer r;
        String p;

        public Coin(Integer r,String p) {
            // TODO Auto-generated constructor stub
            this.r = r;
            this.p = p;
        }

        @Override
        public int compareTo(Coin test) {
            // TODO Auto-generated method stub
            return this.r - test.r;
        }
    }

你的 CoinSelectionSorted 类可能是这样......

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CoinSelectionSorter{

....

    public void sort() {

       Coin[] list =  new Coin[] {new Coin(2, "Johan"),
                                    new Coin(5, "peter"),
                                    new Coin(1, "robin"),
                                    new Coin(15, "walker"),
                                    }; // example data;

       List <Coin> p = Arrays.asList(list);

       Collections.sort(p);
       System.out.println(p); // only good print if you have implement ToString in class coin

      list = p.toArray(list); your sorted array.


    }
}
 ....

希望对你有所帮助

祝你好运

【讨论】:

    猜你喜欢
    • 2021-11-20
    • 1970-01-01
    • 1970-01-01
    • 2012-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多