【问题标题】:Bubble sort is not sorting冒泡排序不是排序
【发布时间】:2013-11-04 02:59:04
【问题描述】:

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

我已经盯着这个看了好几个小时,离开又回来了很多次,但不幸的是,它不起作用!知道为什么吗?!这是我的代码:

//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);
    }       
}     

任何帮助都会很棒!提前致谢!

【问题讨论】:

  • 只切换对象而不是重新分配每个字段不是更好吗?
  • @JoshM 怎么样?对不起,我有点菜鸟...
  • 为什么所有这些类型转换?每次都需要对它进行类型转换的“显示”是什么? (我的意思是,是的,这显然是一个列表,但列表是什么?)
  • 你比较和交换不同的对象,比较 j 和 j + 1 并交换 i 和 j。
  • @pavel.lazar 这就是我老师的例子的样子!我跟着它,现在我不确定。所以我读了一些关于它的东西,但基本上都是一样的。我如何解决它? :(

标签: java sorting


【解决方案1】:

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

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

  • 比较订购
  • 创建临时变量
  • 将左值放入临时变量中
  • 将右值放入左值
  • 将旧的左值从临时值放入右值

您正在对字段进行改组,这导致混乱和错误。请改用上述方法。

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

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);
            }
        }
    }
}

【讨论】:

    【解决方案2】:

    对于执行此操作的通用方法,也许您可​​以尝试以下方法:

    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);
    }
    

    【讨论】:

      【解决方案3】:

      你如何解决它?

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

      有两点:

      • 交换同一索引,在内部for(索引j
      • 正确的交换:你有j的地方写j+1和你有i的地方写j
      • 另一个for 就是这样它会迭代足够多的时间以在最坏的情况下对其进行排序(其他答案中的建议是while,更好)

      话虽如此,交换伪代码是:

      if (show[j] < show[j+1]) {
          temp = j+1
          j+1 = j
          j = temp
      }
      

      这是带有修复的交换代码:

              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,所以我们在第一个整数上排序):

      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
      

      【讨论】:

        【解决方案4】:
        public class myBubbleSort
        {
            private static int[] a;
        
            public static void main(String[] args)
            {
                getArray(10);
                System.out.println("Array before sorting");
                printArray();
                ascendingBubble();
                System.out.println("Array after ascending sort");
                printArray();
                descendingBubble();
                System.out.println("Array after descending sort");
                printArray();
        
                System.out.println();
                System.out.println("Random sort");
                getArray(10);
                bubbleSort(true);
                System.out.println("Array after Random sort");
                printArray();
            }
        
            // print the number in random array
            public static void printArray()
            {
                for (int i : a)
                {
                    System.out.print(i + " ");
                }
                System.out.println();
            }
        
            // generate a random array to be sorted in ascending and descending order
            public static void getArray(int size)
            {
                a = new int[size];
                int item = 0;
                for (int i = 0; i < size; i++)
                {
                    item = (int) (Math.random() * 100);
                    a[i] = item;
                }
            }
        
            // sort getArray in ascending order and bubblesort it
            public static void ascendingBubble()
            {
                int temp;
                System.out.println();
                System.out.println("Ascending sort");
                for (int i = 0; i < a.length - 1; i++)
                {
                    for (int j = 0; j < a.length - 1; j++)
                    {
                        if (a[j] > a[j + 1])
                        {
                            temp = a[j];
                            a[j] = a[j + 1];
                            a[j + 1] = temp;
                        }
                    }
                }
        
                bubbleSort(true);
            }
        
            // sort getArray in descending order and bubblesort it
            public static void descendingBubble()
            {
                int temp;
                System.out.println();
                System.out.println("Descending sort");
        
                for (int i = 0; i < a.length - 1; i++)
                {
                    for (int j = 0; j < a.length - 1; j++)
                    {
                        if (a[j] < a[j + 1])
                        {
                            temp = a[j];
                            a[j] = a[j + 1];
                            a[j + 1] = temp;
                        }
                    }
                }
        
                bubbleSort(true);
            }
        
            // bubble sort algorithm
            public static void bubbleSort(boolean printTime)
            {
                boolean sorted = false;
                int pass = 1;
                int temp;
                long startTime;
                long endTime;
                long duration;
        
                startTime = System.nanoTime();
                while (pass < a.length - 1 && (!sorted))
                {
                    sorted = true;
                    for (int i = 0; i < a.length - 1; i++)
                    {
                        if (a[i] > a[i + 1])
                        {
                            temp = a[i];
                            a[i] = a[i + 1];
                            a[i + 1] = temp;
                            sorted = false;
                        }
                    }
                    pass = pass + 1;
                }
                endTime = System.nanoTime();
                duration = (endTime - startTime);
                if(printTime)
                {
                    System.out.println(duration + " "+ " nano seconds");
                }
            }
        
        }
        

        【讨论】:

        • 你改变了什么?问题出在哪里?请尝试格式化您的代码。
        猜你喜欢
        • 1970-01-01
        • 2013-10-09
        • 2017-10-21
        • 1970-01-01
        • 2014-06-25
        • 2021-12-15
        • 2016-02-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多