冒泡排序法

 //输入全班同学的年龄,按年龄从大到小排列
            Console.Write("请输入人数");
            int n = int.Parse(Console.ReadLine());
            int[] nianling = new int[n];
         
            for (int i = 0; i < n; i++)
            {

                Console.Write("请输入第{0}个人的年龄",i+1);
                nianling[i] = int.Parse(Console.ReadLine());
            }
            //开始排序

            for (int j = 0; j < n - 1; j++)
            {
                for (int i = j+1; i < n; i++)
                {
                    if (nianling[i] > nianling[j])
                    {
                        //等量代换
                        int zhong = nianling[i];
                        nianling[i] = nianling[j];
                        nianling[j] = zhong;
                    }
                }
            }

            foreach (int i in nianling)
            {
                Console.WriteLine(i);
            }
            Console.ReadLine();

  

posted @ 2015-11-23 11:07  小菜鸟。  阅读(207)  评论(0编辑  收藏  举报