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中的數字仍停留在原來的訂單後的數字是圍繞互換,然後處理只是重演。
我該如何糾正這種情況。
親切的問候