AI智能
改变未来

C#学习.冒泡排序法

   //从小到大排序

      static void Main(string[] args)
        {
            int[] nums = { 1, 3, 5, 4, 6, 2 };
            for (int i = 0; i < nums.Length-1; i++)
            {
                for (int j = 0; j < nums.Length – i-1; j++)
                {
                    if (nums[j] > nums[j + 1])
                    {
                        int num;
                        num = nums[j];
                        nums[j] = nums[j + 1];
                        nums[j + 1] = num;
                    }
                }
            }
            for (int i = 0; i < nums.Length; i++)
            {
                Console.WriteLine(nums[i]);
            }
         
            Console.Read();
        }

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » C#学习.冒泡排序法