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
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
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
分类:
08.Database
, 02.FrameWork/C#
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· Open-Sora 2.0 重磅开源!