C# 控制台应用小程序(一)排序问题

C# 控制台应用小程序(一)排序问题

冒泡排序:

代码

 static void Main(string[] args)
        {
            #region 排序
            int[] nums = new int[5];
            Console.WriteLine("请输入{0}个数", nums.Length);
            for (int i = 0; i < nums.Length; i++)
            {
                nums[i] = int.Parse(Console.ReadLine());
            }
            for (int i = 0; i < nums.Length; i++)
            {
                for (int j = 0; j < nums.Length - 1; j++)
                {
                    if (nums[j] < nums[j + 1])
                    {
                        int temp = nums[j];
                        nums[j] = nums[j + 1];
                        nums[j + 1] = temp;
                    }
                }
            }
            for (int i = 0; i < nums.Length; i++)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Write(nums[i] + "ha");
            }
            Console.ReadLine();
            #endregion

猜你喜欢

转载自blog.csdn.net/qq_38708766/article/details/82630073