java - 如何调试我的冒泡排序代码?

标签 java algorithm sorting

<分区>

我在大学里得到了这个冒泡排序并且正在尝试运行它!它应该按照大小从小到大的顺序对数字进行排序。任何帮助,将不胜感激。 (它来自 YouTube 上的 Derek Banas)。你知道为什么它不起作用吗?

public class ListForSorting {

    int arraySize = 10;
    int[] myArray = { 10, 12, 3, 4, 50, 60, 7, 81, 9, 100 };

    public void printArray() {

        System.out.println("----------");
        for (int i = 0; i < arraySize; i++) {

            System.out.print("| " + i + " | ");
            System.out.println(myArray[i] + " |");

            System.out.println("----------");

        }

    }

    public static void main(String[] args) {
        ListForSorting list = new ListForSorting();

        list.printArray();

        list.bubbleSort();
        list.printArray();
    }

    public void bubbleSort() {

        for (int i = arraySize - 1; i > 1; i--) {
            for (int j = 0; j < i; j++) {
                if (myArray[j] < myArray[j + 1])
                    swap(j, j + 1);

            }
        }

    }

    public void swap(int indexOne, int indexTwo) {
        int temp = myArray[indexOne];
        myArray[indexOne] = myArray[indexTwo];
        temp = myArray[indexTwo];
    }
}

输出:

----------
| 0 | 10 |
----------
| 1 | 12 |
----------
| 2 | 3 |
----------
| 3 | 4 |
----------
| 4 | 50 |
----------
| 5 | 60 |
----------
| 6 | 7 |
----------
| 7 | 81 |
----------
| 8 | 9 |
----------
| 9 | 100 |
----------
----------
| 0 | 81 |
----------
| 1 | 100 |
----------
| 2 | 100 |
----------
| 3 | 100 |
----------
| 4 | 100 |
----------
| 5 | 100 |
----------
| 6 | 100 |
----------
| 7 | 100 |
----------
| 8 | 100 |
----------
| 9 | 100 |

谢谢!

最佳答案

您的 swap 函数有误。应该是这样的:

public void swap(int indexOne, int indexTwo) {
    int temp = myArray[indexOne];
    myArray[indexOne] = myArray[indexTwo];
    myArray[indexTwo] = temp;
}

另外,bubblesort 函数有错误。计数器 i 应该下降到 1 而不是 2,否则你的第一个元素将不会按应有的顺序排序:

public void bubbleSort() {

        for (int i = arraySize - 1; i > 0; i--) {
            for (int j = 0; j < i; j++) {
                if (myArray[j] < myArray[j + 1])
                    swap(j, j + 1);

            }
        }

    }

现在输出:

----------
| 0 | 10 |
----------
| 1 | 12 |
----------
| 2 | 3 |
----------
| 3 | 4 |
----------
| 4 | 50 |
----------
| 5 | 60 |
----------
| 6 | 7 |
----------
| 7 | 81 |
----------
| 8 | 9 |
----------
| 9 | 100 |
----------
----------
| 0 | 100 |
----------
| 1 | 81 |
----------
| 2 | 60 |
----------
| 3 | 50 |
----------
| 4 | 12 |
----------
| 5 | 10 |
----------
| 6 | 9 |
----------
| 7 | 7 |
----------
| 8 | 4 |
----------
| 9 | 3 |
----------

如果你想从最小的数到最大的数,只需改变 bubblesort 函数中的 if 条件:

public void bubbleSort() {

        for (int i = arraySize - 1; i > 0; i--) {
            for (int j = 0; j < i; j++) {
                if (myArray[j] > myArray[j + 1])
                    swap(j, j + 1);

            }
        }

    }

现在,输出将是:

----------
| 0 | 10 |
----------
| 1 | 12 |
----------
| 2 | 3 |
----------
| 3 | 4 |
----------
| 4 | 50 |
----------
| 5 | 60 |
----------
| 6 | 7 |
----------
| 7 | 81 |
----------
| 8 | 9 |
----------
| 9 | 100 |
----------
----------
| 0 | 3 |
----------
| 1 | 4 |
----------
| 2 | 7 |
----------
| 3 | 9 |
----------
| 4 | 10 |
----------
| 5 | 12 |
----------
| 6 | 50 |
----------
| 7 | 60 |
----------
| 8 | 81 |
----------
| 9 | 100 |
----------

编辑:性能

您可以通过“检查”内部循环是否至少进行了一次交换来加快速度。如果没有,则意味着您可以在外循环完成后存在它,并为其余的外循环迭代节省时间。

关于java - 如何调试我的冒泡排序代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36922898/

相关文章:

java - 如何比较单词中的字符? ( java )

java - 使用多线程的二进制搜索

java - 将字符串中的 [] 替换为 ()

基于票数和 5 星评级的对象评级算法

c++ - 在小于 O(N) 的时间内找出点是否在 N 个(可能重叠的)矩形之一内

algorithm - 有没有比 Bogosort(又名猴子排序)更糟糕的排序算法?

java - Iterable<Key> 作为返回类型,这是什么意思?

algorithm - 给定 2 个 3D 中的非交叉多边形,它们都可以通过 View 射线 Oz 看到,确定哪个在前面

java - 对对象 HashMap 进行排序(不按键)

c++ - 插入排序怎么写