我在int数组上应用冒泡排序它正在工作但是在控制台(输出)上我希望完全交换例如int数组:[3 2 5 4 1]并且所需的输出应该是:
2 3 5 4 1
2 3 4 5 1
2 3 4 1 5
2 3 1 4 5
最后排序的int数组
1 2 3 4 5.
这是代码。
int[] b = { 3, 2, 5, 4, 1 };
int c;
for (int p = 0; p <= b.Length - 2; p++)
{
for (int i = 0; i <= b.Length - 2; i++)
{
if (b[i] > b[i + 1])
{
c = b[i + 1];
b[i + 1] = b[i];
b[i] = c;
}
}
}
foreach (int aa in b)
Console.Write(aa + " ");
Console.ReadLine();
答案 0 :(得分:4)
喜欢这个吗?
int[] b = { 3, 2, 5, 4, 1 };
int c;
for (int p = 0; p <= b.Length - 2; p++)
{
for (int i = 0; i <= b.Length - 2; i++)
{
if (b[i] > b[i + 1])
{
c = b[i + 1];
b[i + 1] = b[i];
b[i] = c;
foreach (int aa in b)
Console.Write(aa + " ");
Console.WriteLine();
}
}
}
Console.ReadLine();