zoukankan      html  css  js  c++  java
  • 记录排序算法

    原文链接:https://www.cnblogs.com/PJQOOO/p/11669493.html

    感觉各种算法真实太神奇了

            static void Main(string[] args)
            {
                int count = 0;
    
                M: MyArrayList mal = new MyArrayList();
                System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
                watch.Start();//开始计时
                int[] array = mal.CreateArray(100000,1000000);
                watch.Stop();
                //Console.WriteLine(String.Join(",", array));
                Console.WriteLine("数组数量(" + array.Length + ")-------------创建数组耗时:" + watch.ElapsedMilliseconds + "毫秒");
    
                watch = new System.Diagnostics.Stopwatch();
                watch.Start();
                int[] array1 = (int[])array.Clone();
                //冒泡排序
                for (int i = 0; i < array1.Length - 1; i++)
                {
                    for (int j = i + 1; j < array1.Length; j++)
                    {
                        if (array1[i] < array1[j])
                        {
                            int temp = array1[i];
                            array1[i] = array1[j];
                            array1[j] = temp;
                        }
                    }
                }
                watch.Stop();
                //Console.WriteLine(String.Join(",", array1));
                Console.WriteLine("数组数量(" + array1.Length + ")---------------冒泡排序耗时:" + watch.ElapsedMilliseconds + "毫秒");
    
                int[] array2 = (int[])array.Clone();
                watch = new System.Diagnostics.Stopwatch();
                watch.Start();
                //选择排序
                for (int i = 0; i < array2.Length - 1; i++)
                {
                    int maxIndex = i;
                    for (int j = i + 1; j < array2.Length; j++)
                    {
                        if (array2[j] > array2[maxIndex])
                        {
                            maxIndex = j;
                        }
                    }
                    int temp = array2[i];
                    array2[i] = array2[maxIndex];
                    array2[maxIndex] = temp;
                }
                watch.Stop();
                //Console.WriteLine(String.Join(",", array2));
                Console.WriteLine("数组数量(" + array2.Length + ")---------------选择排序耗时:" + watch.ElapsedMilliseconds + "毫秒");
    
                int[] array3 = (int[])array.Clone();
                watch = new System.Diagnostics.Stopwatch();
                watch.Start();
                //插入排序
                for (int i = 1; i < array3.Length; i++)
                {
                    int current = array3[i];
                    int preIndex = i - 1;
                    while (preIndex >= 0 && current > array3[preIndex])
                    {
                        array3[preIndex + 1] = array3[preIndex];
                        preIndex--;
                    }
                    array3[preIndex + 1] = current;
                }
                watch.Stop();
                //Console.WriteLine(String.Join(",", array3));
                Console.WriteLine("数组数量(" + array3.Length + ")---------------插入排序耗时:" + watch.ElapsedMilliseconds + "毫秒");
    
                int[] array4 = (int[])array.Clone();
                watch = new System.Diagnostics.Stopwatch();
                watch.Start();
                //希尔排序
                int current1, gap = array4.Length / 2;
                while (gap > 0)
                {
                    for (int i = gap; i < array4.Length; i++)
                    {
                        current1 = array4[i];
                        int preIndex = i - gap;
                        while (preIndex >= 0 && array4[preIndex] > current1)
                        {
                            array4[preIndex + gap] = array4[preIndex];
                            preIndex -= gap;
                        }
                        array4[preIndex + gap] = current1;
                    }
                    gap /= 2;
                }
                watch.Stop();
                //Console.WriteLine(String.Join(",", array4));
                Console.WriteLine("数组数量(" + array4.Length + ")---------------希尔排序耗时:" + watch.ElapsedMilliseconds + "毫秒");
    
                //循环次数
                if (count < 100)
                {
                    Console.WriteLine("-----------------------------------------------------");
                    count += 1;
                    goto M;
                }
                string word = Console.ReadLine();
                if (word == "more")
                {
                    goto M;
                }
            }
            /// <summary>
            /// 创建随机数量的随机数组
            /// </summary>
            /// <param name="count">数组长度</param>
            /// <param name="number">随机数最大值(最小值默认为1)</param>
            /// <returns></returns>
            public int[] CreateArray(int count,int number)
            {
                int length = new Random().Next(1, count);
                int[] array=new int[length];
                Random random = new Random();
                for (int i = 0; i < length; i++)
                {
                    int item = random.Next(1, number);
                    array[i] = item;
                }
                return array;
            }

     才疏学浅只能看懂到希尔排序

  • 相关阅读:
    2018 ACM 网络选拔赛 徐州赛区
    2018 ACM 网络选拔赛 焦作赛区
    2018 ACM 网络选拔赛 沈阳赛区
    poj 2289 网络流 and 二分查找
    poj 2446 二分图最大匹配
    poj 1469 二分图最大匹配
    poj 3249 拓扑排序 and 动态规划
    poj 3687 拓扑排序
    poj 2585 拓扑排序
    poj 1094 拓扑排序
  • 原文地址:https://www.cnblogs.com/jianghaidong/p/11685261.html
Copyright © 2011-2022 走看看