首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >选择排序出错

选择排序出错
EN

Stack Overflow用户
提问于 2015-11-29 23:18:36
回答 1查看 22关注 0票数 0

我试图使用数组上的可比排序来进行选择排序。我不知道它为什么不起作用。如果有人能看一看,帮我找出不起作用的东西,那就太好了!谢谢!

代码语言:javascript
复制
public static Comparable[] no = new Comparable[100];

public static Comparable[] gen1()
{
    Random random = new Random();
    for(int i=0;i<no.length;i++)
    {
        no[i] =random.nextInt();
    }
    return no;
}   

public static Comparable[] selectionSort (Comparable no[])
   {
      int min;
      Comparable temp;

      for (int index = 0; index < no.length-1; index++)
      {
         min = index;
         for (int scan = index+1; scan < no.length; scan++)
            if (no[scan].compareTo(no[min]) < 0)
               min = scan;
         temp = no[min];
         no[min] = no[index];
         no[index] = temp;
      }
      return no;
   }

public static void main(String[] args)
{
    System.out.println("Original Array:");
    System.out.println(Arrays.toString(gen1()));
    System.out.println("Sorted Array:");
    System.out.println(selectionSort(no));

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-29 23:28:44

您没有指定您的问题是什么,但在主体的最后一行中,您应该这样做

代码语言:javascript
复制
  System.out.println(Arrays.toString(selectionSort(no)));

输出在我看来是排序的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33989313

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档