继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Linq排序效率 Vs 快速排序效率

吃鸡游戏
关注TA
已关注
手记 297
粉丝 55
获赞 339


 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毫秒

二者几乎差不多

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP