如何调试我的冒泡排序代码? [英] How to debug my Bubble Sort code?
问题描述
我在大学里得到了这个bubort,我正试图运行它!它应该按照大小,从最小到最大的顺序对数字进行排序。任何帮助,将不胜感激。 (它来自YouTube上的Derek Banas)。你知道为什么它不起作用吗?
I got this bubblesort in college and am trying to run it! It's supposed to sort the numbers in order of size, smallest to largest. Any help would be appreciated. (It comes from Derek Banas on YouTube). Do you know why it doesn't work?
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
函数错误。它应该是这样的:
Your swap
function was wrong. It should be like this:
public void swap(int indexOne, int indexTwo) {
int temp = myArray[indexOne];
myArray[indexOne] = myArray[indexTwo];
myArray[indexTwo] = temp;
}
此外, bubblesort
功能有错误。计数器 i
应该降至 1
而不是 2
,或者你的第一个元素不应该按照它应该排序:
Also, bubblesort
function has an error. Counter i
should go down to 1
not to 2
, or else your first element wont be sorted as it should be:
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 |
----------
如果你想从最小的数字到最大的,只需更改 if
条件 bubblesort
功能:
If you want from smallest number to biggest, just change if
condition in bubblesort
function:
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 |
----------
编辑:效果
您可以通过检查内部循环是否至少执行一次交换来加快速度。如果不是这意味着你可以存在外循环,因为它完成并节省了外循环迭代的其余时间。
You can make this faster by "checking" if inner loop did at least one swap. If it didn't that means you can exist the outer loop since it finished and save time for the rest of outer loop iteration.
这篇关于如何调试我的冒泡排序代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!