zoukankan      html  css  js  c++  java
  • 二分法和冒泡法的比较(快速排序法)

    在为一家xx旅行网,因为要标出最惠价格,而前辈人员用的是冒泡法的方法,这里我把冒泡法和二分法做个小比较。

    using System.Diagnostics; //用来测试时间单位

    static void Main(string[] args)
            {

                List<int> intA = new List<int>(); // 假如有个列表对象,
                while (intA.Count() < 1500)         // 这个列表对象里面有1500条不相同的记录
                {
                    Random r = new Random();
                    int num = r.Next(0,3000);
                    if (!intA.Contains(num))
                    {
                        intA.Add(num);
                        Console.Write(num + ",");
                    }               
                }       

                Stopwatch st = new Stopwatch();//实例化类 由using System.Diagnostics的来的

                st.Start();
                Console.WriteLine("\n二分法最大数为:"+GetMax(intA, 0, intA.Count()));   
                st.Stop();
                Console.WriteLine("\n二分法执行时间" +st.ElapsedMilliseconds.ToString() );

                st.Start();
                Console.WriteLine("\n快速法最大数为:"+ForGetMax(intA));
                st.Stop();
                Console.WriteLine("\n快速法执行时间" + st.ElapsedMilliseconds.ToString());

                Console.ReadKey();

    }

           public static int mid;          

            public static int GetMax(List<int> array, int first, int end)
            {
                int frontHalf, backHalf;          
                mid=(first+end)/2;
                if (first == mid) return array[first];
                frontHalf = GetMax(array, first, mid);
                backHalf = GetMax(array, mid + 1, end);       

                return frontHalf > backHalf ? frontHalf : backHalf;
            }

            public static int ForGetMax(List<int> array)
            {
                int temp = 0;
                if(array.Count()>0)
                {
                    temp = array[0];
                    for (int m = 0; m < array.Count(); m++)
                    {
                        if (temp < array[m]) { temp = array[m]; }
                    }
                }

                return temp;        
            }

    -------------------------华丽的分解线-------------------

    用1500条记录,执行得到结果,所花费的时间:(这里在记录数好像是350后,两个方法所用的时间就不同了)

  • 相关阅读:
    SpringBoot-Mysql模板多数据源加载
    SpringCloud-动态配置变化监控-获取变化(支持Config、Nacos)
    SpringBoot-ElasticJob封装快速上手使用(分布式定时器)
    关键字(标签)提示组件——拼音、汉字混合搜索
    写一个高性能的敏感词检测组件
    一个文件搞定Asp.net core 3.1动态页面转静态页面
    浅谈C#在网络波动时防重复提交
    对RC4算法进行改写,新的加密算法RCX。
    【ToolGood.Words】之【StringSearch】字符串搜索——基于BFS算法
    万能解决方案之彻底解决macOS cocoapods环境的所有问题
  • 原文地址:https://www.cnblogs.com/liuming8208/p/2478378.html
Copyright © 2011-2022 走看看