Reputation: 5
I am applying bubble sort on an int array and it is working but on console (output) i want complete swapping for example int array:[3 2 5 4 1] and required output should be :
2 3 5 4 1
2 3 4 5 1
2 3 4 1 5
2 3 1 4 5
and in last the sorted int array
1 2 3 4 5.
Here is the code.
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();
Upvotes: 0
Views: 285
Reputation: 7787
Like this?
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();
Upvotes: 4