冒泡排序

发布于 2020-07-18  195 次阅读


            //冒泡排序
            int[] nums = { 9, 8, 5, 7, 6, 4, 3, 2, 1, 0 };
          //外层控制循环数
            for (int i = 0; i < nums.Length - 1; i++)  
            {
                for (int j = 0; j < nums.Length - 1 - i; 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.WriteLine(nums[i]);
            }
            Console.ReadKey();
            int temp2 = 0;
            int[] num = { 9, 8, 5, 7, 6, 4, 3, 2, 1, 0 };
            for (int i = 0; i < num.Length - 1; i++)
            {
                for (int j = 0; j < num.Length - 1; j++)
                {
                    if (num[j] > num[j + 1])
                    {
                        temp2 = num[j];
                        num[j] = num[j + 1];
                        num[j + 1] = temp2;
                    }
                }
            }
            for (int i = 0; i < num.Length; i++)
            {
                Console.WriteLine(num[i]);
            }
            Console.ReadKey();