首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >冒泡排序不是排序

冒泡排序不是排序
EN

Stack Overflow用户
提问于 2013-11-04 10:59:05
回答 4查看 230关注 0票数 0

我已经编写了一个代码来输入一些节目的名称、日期和时间,并可以选择按日期和名称进行排序(冒泡排序)。我使用1.4.2 (因为我必须这样做)和一个ArrayList以及一个简单的类。

我已经盯着它看了几个小时了,离开了又回来了一堆次,但不幸的是,它不工作!知道为什么吗?!下面是我的代码:

代码语言:javascript
复制
//method to sort and display info
public static void sortDay(){          
    for(int i = 0; i < show.size() - 1; i++) {
        for(int j = 0; j < show.size() - 1; j++){
            showInfo current = (showInfo)show.get(j);
            showInfo next = (showInfo)show.get(j+1);

            if (current.day.compareTo(next.day) < 0) {
                showInfo temp = new showInfo();
                temp.name = ((showInfo)show.get(j)).name;
                temp.day = ((showInfo)show.get(j)).day;
                temp.time = ((showInfo)show.get(j)).time;

                ((showInfo)show.get(j)).time = ((showInfo)show.get(i)).time;
                ((showInfo)show.get(j)).day = ((showInfo)show.get(i)).day;
                ((showInfo)show.get(j)).name = ((showInfo)show.get(i)).name;

                ((showInfo)show.get(i)).time = temp.time;
                ((showInfo)show.get(i)).day = temp.day;
                ((showInfo)show.get(i)).name = temp.name;
            }
        } 
    }
    System.out.println("Show Information");
    for (int i = 0; i < show.size(); i++){
        System.out.println("Name: " + ((showInfo)show.get(i)).name);
        System.out.println("Day: " + ((showInfo)show.get(i)).day);
        System.out.println("Time: " + ((showInfo)show.get(i)).time);
    }       
}     

任何帮助都是最好的!提前感谢!

EN

回答 4

Stack Overflow用户

发布于 2013-11-04 11:15:19

首先,我假设您使用的是某种类型的List --可能是ArrayList

也就是说,冒泡排序的主要操作如下:

  • 比较ordering
  • Create临时变量
  • 将左值放入临时变量
  • 将右值放入左值
  • 将旧的左值放入临时值的右值

您在字段之间来回移动,这将导致混乱和bug。改用上面的方法。

这里使用泛型(所以您不必再进行强制转换)和一个大写的类名进行了说明,这是惯例。在本例中我没有临时变量,因为我已经有了一个对current的引用。

代码语言:javascript
复制
List<ShowInfo> show = new ArrayList<>(); // assume populated

public static void sortDay(){
    for(int i = 0; i < show.size(); i++) {
        for(int j = 0; j < show.size() && j != i; j++) {
            ShowInfo current = show.get(i);
            ShowInfo next = show.get(j);

            // If the current day is greater than the next day, we need to swap.
            // Adjust to suit your business logic (if current is less than next).
            if (current.day.compareTo(next.day) > 0) {
                show.set(i, next);
                show.set(j, current);
            }
        }
    }
}
票数 0
EN

Stack Overflow用户

发布于 2013-11-04 11:37:24

对于一种通用的方法,也许您可以尝试如下所示:

代码语言:javascript
复制
public static <T extends Comparable> void sort(final List<T> list){
    boolean remaining;
    do{
        remaining = false;
        for(int i = 0; i < list.size()-1; i++){
            final T current = list.get(i);
            final T next = list.get(i+1);
            if(current.compareTo(next) < 0){
                list.set(i, next);
                list.set(i+1, current);
                remaining = true;
            }
        }
    }while(remaining);
}
票数 0
EN

Stack Overflow用户

发布于 2016-12-21 22:37:56

你怎么解决它呢?

我只是在回答你的问题:如何修复你发布的代码。关于“如何改进它?”所有其他的答案都比我能想到的要好得多。

这里有两点:

在相同的索引上进行交换,在内部交换( index j)

  • correct for:where you j write j+1和where you have i write j

  • the other for只是为了在最坏的情况下迭代足够多的次数来进行排序(其他答案中的建议是while,更好)

也就是说,交换伪代码是:

代码语言:javascript
复制
if (show[j] < show[j+1]) {
    temp = j+1
    j+1 = j
    j = temp
}

下面是带有修复的交换代码:

代码语言:javascript
复制
        if (current.day.compareTo(next.day) < 0) {
            showInfo temp = new showInfo();
            temp.name = ((showInfo)show.get(j+1)).name;
            temp.day = ((showInfo)show.get(j+1)).day;
            temp.time = ((showInfo)show.get(j+1)).time;

            ((showInfo)show.get(j+1)).time = ((showInfo)show.get(j)).time;
            ((showInfo)show.get(j+1)).day = ((showInfo)show.get(j)).day;
            ((showInfo)show.get(j+1)).name = ((showInfo)show.get(j)).name;

            ((showInfo)show.get(j)).time = temp.time;
            ((showInfo)show.get(j)).day = temp.day;
            ((showInfo)show.get(j)).name = temp.name;
        }

下面是打印的结果(假设每个节目都是day - time - name的,所以我们是在第一个int上排序):

代码语言:javascript
复制
Show Information before sort
610 - -72 - 1402
838 - -184 - 1096
-478 - 248 - 934
709 - 832 - -590
2007 - 954 - -315
Show Information after sort
2007 - 954 - -315
838 - -184 - 1096
709 - 832 - -590
610 - -72 - 1402
-478 - 248 - 934
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19760980

复制
相关文章

相似问题

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