总的来说,每当我调用此方法对数组进行排序时,它都会停止,就像在等待响应一样。知道为什么它不起作用吗?
public void bubbleSort(){
boolean finished = false;
boolean swapOccurred = false;
int i = 0;
int next = i + 1;
while (finished == false)
{
for (; i < theArray.length - 1; i++)
{
if (theArray[i] > theArray[next])
{
swapValues(theArray[i], theArray[next]);
swapOccurred = true;
}
}
if (swapOccurred == false)
{
finished = true;
}
}
}
private void swapValues(int i, int next) {
int temp;
temp = i;
i = next;
next = temp;
}
最佳答案
如果是java,则
- 下一个索引永远不会更新,因此它始终保持为 1。
- swap 方法没有副作用,您想要交换 theArray 中的元素,但在 java 方法中,参数作为值传递,因此 next 和
i
变量仅在 swap 方法内更改值,它们与 theArray 数组的单元格无关。
关于java - bubbleSort 和 swap 方法不起作用 [Java],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28777022/