从代码的世界路过

C# 冒泡算法

2016-09-26  本文已影响3人  ainiok
int[] scores = { 99, 85, 74, 84, 64, 93, 91 ,87};
            int temp;
            for (int i = 0; i < scores.Length; i++)
            {
                for (int j = 0; j < scores.Length - 1 - i; j++) 
                {
                    if (scores[j] > scores[j + 1])
                    {
                        //交换元素
                        temp = scores[j];
                        scores[j] = scores[j + 1];
                        scores[j + 1] = temp;
                    }
                }
            }
            Console.WriteLine("排序后的成绩为:");
            foreach (var item in scores)
            {
                Console.WriteLine("{0}\t",item.ToString());
            }
上一篇 下一篇

猜你喜欢

热点阅读