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


     

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

    }  


  • 相关阅读:
    自定义IP原来如此简单
    [转]如何在NIOS II中读写EPCS剩余空间
    坏了的芯片居然又好了一片,太神奇了
    今天报废两片EP3C5E144
    Quartus II 订购版 v10.1 正式推出下载
    发现用JTAG下载程序到EPCS比用AS方式下载速度快
    如何解决No EPCS layout data looking for section [EPCSXXXXXX]
    QII丰衣足食
    Why does my Cyclone III FPGA fail to access the EPCS device using the EPCS Controller module?
    <转载>在.NET中基于Windows消息的IPC实现
  • 原文地址:https://www.cnblogs.com/zhuiyi/p/2079143.html
Copyright © 2011-2022 走看看