zoukankan      html  css  js  c++  java
  • 冒泡排序2

    class Program
        {
            public int temp;
            /// <summary>
            /// 排序
            /// </summary>
            /// <param name="array"></param>      
            public  void Sequence(List<int> array)
            {
                for (int i = 0; i < array.Count-1; i++)
                {
                    for (int j = 0; j < array.Count-i-1; j++)
                    {
                        if (array[j]<array[j+1])
                        {
                            temp = array[j];
                            array[j] = array[j + 1];
                            array[j + 1] = temp;
                        }
                    }
                }
            }
            /// <summary>
            /// 遍历
            /// </summary>
            /// <param name="array"></param>
            public void Traversal(List<int> array)
            {
                foreach (var item in array)
                {
                    Console.Write(item+" ");
                }
            }
            /// <summary>
            /// 确定数组长度
            /// </summary>
            /// <param name="n"></param>
            public void CreateArray(int n, List<int> num)
            {
                Random rand = new Random();
                for (int i = 0; i < n; i++)
                {
                    num.Add(rand.Next(0, 101));
                }
            }
            static void Main(string[] args)
            {
                Program p;
                p = new Program();
                List<int> num = new List<int>();
                Random rand = new Random();
                Console.WriteLine("请输入要创建的泛型数组的长度");
                string length = Console.ReadLine();
                uint s;
                if (!uint.TryParse(length,out s))
                {
                    Console.WriteLine("输入有误,请重新输入!");
                    return;
                }
                try
                {
                    p.CreateArray(Convert.ToInt32(s), num);
                    Console.WriteLine("原数组是:");
                    p.Traversal(num);
                    Console.WriteLine();
                    p.Sequence(num);
                    Console.WriteLine("排序后的数组是:");
                    p.Traversal(num);
                }
                catch (Exception ex)
                {
                    throw new Exception("报错了!" + ex.Message);
                }
                Console.WriteLine();
                Console.ReadKey();
            }
        }

  • 相关阅读:
    Nginx+.Net Core实现项目负载均衡
    linux(centos8):使用zip/unzip压缩和解压缩文件
    spring boot:用itextpdf处理pdf表格文件(spring boot 2.3.2)
    spring boot:使用poi导出excel电子表格文件(spring boot 2.3.1)
    spring boot:发送带附件的邮件和html内容的邮件(以163.com邮箱为例/spring boot 2.3.2)
    暗夜模式配置
    简述逻辑外键
    分页后端逻辑
    python算法时间复杂度
    python五大排序算法
  • 原文地址:https://www.cnblogs.com/riskyer/p/3297037.html
Copyright © 2011-2022 走看看