zoukankan      html  css  js  c++  java
  • [转]C# 冒泡排序你还会吗?

    本文转自:http://www.cnblogs.com/zhuiyi/archive/2011/06/12/2079143.html


     

    都知道两个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();
            }
        }

    }  

    复制代码
  • 相关阅读:
    【网络】默认路由(路由黑洞,路由终结)
    textstroke(文本描边)和textfillcolor(文本填充色)Css3演示
    enter键提交表单
    Web App和Native App 谁将是未来
    模拟input type=file
    margintop 无效,避开麻烦的margin叠加(margin collapsing)
    用jquery写的简单tab效果
    CSS3下的渐变文字效果实现
    jQuery阻止冒泡和HTML默认操作
    设置文本输入框默认值
  • 原文地址:https://www.cnblogs.com/freeliver54/p/3013803.html
Copyright © 2011-2022 走看看