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后,两个方法所用的时间就不同了)

  • 相关阅读:
    496. 下一个更大元素 I 力扣(简单) 单调栈
    240. 搜索二维矩阵 II 力扣(中等) Z字型查找
    638. 大礼包 力扣(中等) 记忆化搜索,弱点
    453. 最小操作次数使数组元素相等 力扣(简单) 没想出来
    传纸条
    同余方程
    花匠
    华容道
    货车运输
    火柴排队
  • 原文地址:https://www.cnblogs.com/liuming8208/p/2478378.html
Copyright © 2011-2022 走看看