简体   繁体   English

冒泡排序交换(C#)

[英]Bubble sort swapping (C#)

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 : 我在一个int数组上应用冒泡排序,它正在工作,但在控制台(输出)上,我想完成交换,例如int array:[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 

and in last the sorted int array 最后是排序的int数组

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();

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM