冒泡排序 C#

static void Main(string[] args)
        {
            int[] nums = { 5, 10, 3,8,20,7};
            Console.WriteLine("数组长度:"+nums.Length);
            for (int i = 0; i < nums.Length; i++)  //外层循环,控制次数
            {
                for (int j = 0; j < nums.Length-i-1; j++) //内层循环,相临2个数比较
                {
                    int temp;
                    if (nums[j] > nums[j + 1])
                    {
                        temp = nums[j + 1];
                        nums[j + 1] = nums[j];
                        nums[j] = temp;
                    }
                }
            }
            for (int i = 0; i < nums.Length; i++)
            {
                Console.WriteLine(nums[i]);
            }
            Console.ReadKey();
        }

posted @ 2020-09-26 23:29  崇尚技术  阅读(117)  评论(0编辑  收藏  举报