zoukankan      html  css  js  c++  java
  • Linq排序效率 Vs 快速排序效率

     1 using System;
     2 using System.Collections;
     3 using System.Collections.Generic;
     4 using System.Diagnostics;
     5 using System.Linq;
     6 
     7 namespace ConsoleTest
     8 {
     9 
    10     class Program
    11     {
    12         static void Main(string[] args)
    13         {
    14 
    15             List<string> listTest = new List<string>();
    16             for (int i = 1; i <= 500000; i++)
    17             {
    18                 listTest.Add(i.ToString().PadLeft(10'0'));
    19             }
    20 
    21             Stopwatch stopMatch = new Stopwatch();
    22             stopMatch.Start();
    23             List<string> listOrder = listTest.OrderByDescending(c => c).ToList<string>();
    24             stopMatch.Stop();
    25 
    26             Console.WriteLine("Linq排序耗时:\t{0}毫秒", stopMatch.ElapsedMilliseconds.ToString());
    27 
    28             string[] arrTest = listOrder.ToArray();
    29 
    30             stopMatch.Reset();
    31             stopMatch.Start();           
    32             QuickSort(arrTest, 0, arrTest.Length - 1);
    33 
    34             stopMatch.Stop();
    35 
    36             Console.WriteLine("二分法排序耗时:\t{0}毫秒", stopMatch.ElapsedMilliseconds.ToString());
    37             Console.Read();
    38 
    39 
    40         }
    41 
    42         /// <summary>
    43         /// 二分法从小到大排序
    44         /// </summary>
    45         /// <param name="array">需要排序的字符串数组</param>
    46         /// <param name="start">排序元素的起始下标</param>
    47         /// <param name="end">排序元素的结止下标</param>       
    48         public static void QuickSort(string[] array, int start, int end)
    49         {
    50 
    51             //有可能造成start>end   因为递归调用时j+1,可能引起j比end还大1。 另外如果数组是空的,或者输入错误也会出现这种情况
    52             if (end <= start)
    53             {
    54                 return;
    55             }
    56             else
    57             {
    58 
    59                 //取中间元素为中心点,然后移到最右边
    60                 int sign = (start + end) / 2;
    61                 string tmp = array[end];
    62                 array[end] = array[sign];
    63                 array[sign] = tmp;
    64                 int j = start;
    65 
    66                 for (int i = start; i <= end - 1; i++)
    67                 {
    68 
    69                     //小于的元素和标记互换,等于的不能互换,否则会形成死循环                   
    70                     if (array[i].CompareTo(array[end]) == -1)
    71                     {
    72 
    73                         tmp = array[i];
    74                         array[i] = array[j];
    75                         array[j] = tmp;
    76                         j = j + 1;
    77 
    78                     }
    79 
    80                 }
    81 
    82                 //把标记元素和第一个>=它的元素位置互换,这样数组就分成2个部分,一个部分比中心值小,一个部分比中心值大。
    83                 tmp = array[j];
    84                 array[j] = array[end];
    85                 array[end] = tmp;
    86                 QuickSort(array, start, j);
    87                 QuickSort(array, j + 1, end);
    88             }
    89 
    90         }
    91 
    92 
    93     }
    94 }
    95 

    测试结果:

    Linq排序耗时:   2131毫秒
    二分法排序耗时: 2083毫秒

    二者几乎差不多

    作者:菩提树下的杨过
    出处:http://yjmyzz.cnblogs.com
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    SGU 176.Flow construction (有上下界的最大流)
    POJ 2391.Ombrophobic Bovines (最大流)
    poj 1087.A Plug for UNIX (最大流)
    poj 1273.PIG (最大流)
    POJ 2112.Optimal Milking (最大流)
    SGU 196.Matrix Multiplication
    SGU 195. New Year Bonus Grant
    关于multicycle path
    ppt做gif动图
    codeforces 598A Tricky Sum
  • 原文地址:https://www.cnblogs.com/yjmyzz/p/1504736.html
Copyright © 2011-2022 走看看