I can't figure out why the second do statement is not repeating. It appears to be exiting regardless of the while statement being true or false. I feel like it's something small I'm missing, but any help is greatly appreciated. Thanks!
public void ShellSort()
{
int gap = size/2;
do
{
boolean swapflag = true;
do
{
swapflag = false;
for(int s = 0; s < (size-gap); s++)
if (arr[s] > arr[s + gap])
{
int temp = arr[s];
arr[s] = arr[s + gap];
arr[s + gap] = temp;
swapflag = true;
}
} while (swapflag = true);
gap = gap/2;
} while (gap >= 0);
}
答案 0 :(得分:1)
replace below line
while (swapflag = true); //this say that you are always setting swapflag to true
with
while (swapflag);// check condition true or false
答案 1 :(得分:1)
代码中的while语句为while (swapflag = true);
因此,您的交换标志设置为true。内部do-while循环永远不会终止。
该陈述应该是:
while(swapflag==true)
或while (swapflag)
答案 2 :(得分:0)
那是因为你在first while语句中使用了赋值operator =而不是关系运算符==。 =赋值运算符将swapflag设置为true而不是比较。 改变它 而(swapflag ==真) ==是关系运算符,它将检查swapflag是否为true 或者(swapflag)