文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>C# 冒泡排序你还会吗?

C# 冒泡排序你还会吗?

时间:2011-06-12  来源:ゞ追忆o0ゞ


 

 

都知道两个for循环搞定,大家是怎么记的这两个循环?

外层:循环数组长度;  i<数组长度-1 //从0开始循环;

内层:循环排序次数;  j<数组长度-1-i;

 

 

 

备忘代码:

 

using System;
using System.Collections.Generic;
using System.Text;

namespace HelloACCP
{
    /// <summary>
    /// 本程序演示使用二重循环实现数组的冒泡排序算法
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            int[] scores = new int[5];
            int i, j;  // 循环变量
            int temp;  // 临时变量

            // 读入成绩
            Console.WriteLine("请输入5个学员的成绩:");
            for (i = 0; i < 5; i++)
            {
                Console.WriteLine("请输入第{0}个学员的成绩:", i + 1);
                scores[i] = int.Parse(Console.ReadLine());//类型转换  
            }

            // 开始排序
            for (i = 0; i < scores.Length-1; i++)
            {
                for (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("排序后的成绩为:");
            for (i = 0; i < 5; i++)
            {
                Console.Write("{0}\t", scores[i]);
            }

            Console.ReadLine();
        }
    }

}  


相关阅读 更多 +
排行榜 更多 +
苏打世界手游

苏打世界手游

休闲益智 下载
躲避我在行

躲避我在行

角色扮演 下载
星际狩猎

星际狩猎

飞行射击 下载