冒泡排序不排序

时间:2013-11-04 02:59:05

标签: java sorting

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

答案 0 :(得分:0)

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

尽管如此,Bubble Sort的主要操作描述如下:

  • 比较订购
  • 创建临时变量
  • 将左侧值放入临时变量
  • 将正确的值放入左侧值
  • 将旧左值从临时值
  • 放入正确的值

你对这些字段进行了改组, 会导致混乱和错误。请改用上述方法。

这里用泛型来说明(所以你不必再施放)和大写字母名称,这是惯例。在此示例中,我没有临时变量,因为我已经引用了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);
            }
        }
    }
}

答案 1 :(得分: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);
}

答案 2 :(得分:0)

你如何解决?

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

有两点:

  • 在内部for(索引j
  • 中交换相同的索引
  • 正确交换:您jj+1的位置以及i jfor
  • 的位置
  • 另一个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 为每个节目,所以我们在第一个int上排序:

function addRow(tableID) {

    var table = document.getElementById(tableID);

    var rowCount = table.rows.length;
    var row = table.insertRow(rowCount);

    var cell1 = row.insertCell(0);
    var element1 = document.createElement("input");
    element1.type = "text";
    element1.name="itemname";
    cell1.appendChild(element1);

    var cell2 = row.insertCell(1);
    var element2 = document.createElement("input");
    element2.type = "text";
    element2.name = "itemcode";
    cell2.appendChild(element2);

    var cell3 = row.insertCell(2);
    var element3 = document.createElement("input");
    element3.type = "text";
    element3.name = "description";
    cell3.appendChild(element3);

    var cell4 = row.insertCell(3);
    var element4 = document.createElement("input");
    element4.type = "text";
    element4.name = "quantity";
    cell4.appendChild(element4);

    var cell5 = row.insertCell(4);
    var element5 = document.createElement("input");
    element5.type = "text";
    element5.name = "price";
    cell5.appendChild(element5);

    var cell6 = row.insertCell(5);
    var element6 = document.createElement("input");
    element6.type = "file";
    element6.name = "image";
    cell6.appendChild(element6);

    var cell7 = row.insertCell(6);
    var element7 = document.createElement("input");
    element7.type = "checkbox";
    element7.name = "check";


}

function deleteRow(tableID) {
    try {
        var table = document.getElementById(tableID);
        var rowCount = table.rows.length;

        for(var i=0; i<rowCount; i++) {
            var row = table.rows[i];
            var chkbox = row.cells[0].childNodes[0];
            if(null != chkbox && true == chkbox.checked) {
                table.deleteRow(i);
                rowCount--;
                i--;
            }


        }
    }catch(e) {
        alert(e);
    }
}

答案 3 :(得分:-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");
        }
    }

}