排列数字c#代码

时间:2016-02-06 20:44:12

标签: c#

有人可以帮忙修复此代码吗?它应该从最低到最高顺序排列数字。但是我无法运行代码......它也不会打印任何东西。感谢

class Program
{
    static void Main(string[] args)
    {
        int[] IN_AR_Numbers = { 3, 2, 1, 6, 5, 4, 9, 8, 7 };

        PrintArray(IN_AR_Numbers);
        BubbleSort(IN_AR_Numbers);
        Console.ReadLine();
    }

    static void BubbleSort(int[] IN_AR_NumbersSorted)
    {
        int IN_Temp = 0;
        bool BL_HasSwapped = false;
        int IN_Swapcount = 0;

        do
        {
            BL_HasSwapped = false;

            for (int j = 0; j < IN_AR_NumbersSorted.Length - 1; j++)
            {
                if (IN_AR_NumbersSorted[j] > IN_AR_NumbersSorted[j + 1])
                {
                    IN_Temp = IN_AR_NumbersSorted[j + 1];
                    IN_AR_NumbersSorted[j + 1] = IN_AR_NumbersSorted[j];
                    IN_AR_NumbersSorted[j] = IN_Temp;
                    BL_HasSwapped = true;
                    IN_Swapcount++;
                }
            }
        }
        while (BL_HasSwapped);

        PrintArray(IN_AR_NumbersSorted);
    }
}

1 个答案:

答案 0 :(得分:2)

你没有在给出的代码中写出任何内容。

using System;

public class Program
{
    public static void Main(string[] args)
    {
        int[] IN_AR_Numbers = { 3, 2, 1, 6, 5, 4, 9, 8, 7 };

        BubbleSort(IN_AR_Numbers);

        foreach (var i in IN_AR_Numbers)
        {
            Console.WriteLine(i);
        }

        Console.ReadLine();
    }

    static void BubbleSort(int[] IN_AR_NumbersSorted)
    {
        int IN_Temp = 0;
        bool BL_HasSwapped = false;
        int IN_Swapcount = 0;

        do
        {
            BL_HasSwapped = false;

            for (int j = 0; j < IN_AR_NumbersSorted.Length - 1; j++)
            {
                if (IN_AR_NumbersSorted[j] > IN_AR_NumbersSorted[j + 1])
                {
                    IN_Temp = IN_AR_NumbersSorted[j + 1];
                    IN_AR_NumbersSorted[j + 1] = IN_AR_NumbersSorted[j];
                    IN_AR_NumbersSorted[j] = IN_Temp;
                    BL_HasSwapped = true;
                    IN_Swapcount++;
                }
            }
        }
        while (BL_HasSwapped);
    }   
}

给出

  

1 2 3 4 5 6 7 8 9

我删除了代码中没有的PrintArray函数,并添加了一个循环来写出数组:

foreach (var i in IN_AR_Numbers)
{
  Console.WriteLine(i);
}

fiddle