`
kxw953492793kxw
  • 浏览: 9209 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

冒泡排序

阅读更多
冒泡排序就是每一次排序完之后就在对之后的数进行比较,直到数组里的数按照从大(小)排列,这是一个嵌套的循环,外层是比较趟数的循环,内部循环是数组内的数比较。
class Program
    {
        static void Main(string[] args)
        {
            int[] a = { 6, 3, 8, 9, 4 };
            Console.WriteLine("排序前");
            for (int i = 0; i < a.Length; i++)
            {
                Console.WriteLine(a[i]);
            }
            Console.WriteLine("排序后");
            for (int i = 0; i < a.Length-1; i++)//控制比较的趟数,这个循环一次就是比较一次
            {
                for (int j = 0; j < a.Length - i-1; j++)//数组内的数的比较
                {
                    if (a[j] > a[j + 1])
                    {
                        int temp;
                        temp = a[j];
                        a[j] = a[j + 1];
                        a[j + 1] = temp;
                    }
                }             
            }
            for (int i = 0; i < a.Length; i++)
            {
                Console.Write(a[i]);
            }
                Console.ReadKey();
        }
    }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics