我正在做一个冒泡排序练习,我的感觉是它非常接近正确。就像现在一样,我看到了一个永恒的循环。
错在哪里?
static void Main(string[] args)
{
int[] numbers = { 2, 4, 8, 5, 88, 55, 32, 55, 47, 8, 99, 120, 4, 32 };
int temporary;
bool sorted;
do
{
sorted = false;
for (int i = 0; i < numbers.Length - 1; i++)
{
int a = numbers[i];
int b = numbers[i + 1];
if (a > b)
{
temporary = a;
a = b;
b = temporary;
sorted = true;
}
}
Console.WriteLine("sorted");
} while (sorted == true);
foreach (int i in numbers)
{
Console.Write(i + " ");
}
}