1
我有一个练习,我需要使用WebMethod来执行冒泡排序,升序和降序。冒险练习遇到困难
这是我用于升序排序的方法。
[WebMethod]
public Boolean larger(int a, int b)
{
Boolean isLarger;
if (a < b)
{
isLarger = false;
}
else
{
isLarger = true;
}
return isLarger;
}
这是访问方法来执行排序的C#应用程序。 我们只需要对5个数字进行排序。
ArrayList numbersAL = new ArrayList();
for (int i = 0; i < 5; i++)
{
numberInput = Int32.Parse(Console.ReadLine());
numbersAL.Add(numberInput);
}
Boolean swap;
do
{
swap = false;
for (int j = 0; j < numbersAL.Count - 1; j++)
{
Console.WriteLine("output");
int a = (int)numbersAL[j];
int b = (int)numbersAL[j + 1];
result = s.larger(a, b);
if (result)
{
temporary = a;
a = b;
b = temporary;
swap = true;
}
}
} while (swap == true);
有了这个,但是我得到一个无限循环,我想这样做的原因是,在ArrayList中的数字仍停留在原来的订单后的数字是围绕互换,然后处理只是重演。
我该如何纠正这种情况。
亲切的问候