0

我编写了一个代码来输入一些节目的名称、日期和时间,并可以选择按日期和名称对其进行排序(冒泡排序)。我正在使用 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);
    }       
}     

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

4

4 回答 4

0

首先,我假设您正在使用某种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);
            }
        }
    }
}
于 2013-11-04T03:15:18.603 回答
0

对于这样做的通用方法,也许您可​​以尝试以下方法:

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);
}
于 2013-11-04T03:37:24.160 回答
0

你如何解决它?

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

有两点:

  • for在内部(index j)中交换同一索引
  • 正确交换:你jj+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每个节目,所以我们在第一个 int 上排序):

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
于 2016-12-21T14:37:55.890 回答
-3
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");
        }
    }

}
于 2014-12-15T14:13:06.073 回答