LINQ sorting efficiency vs quick sorting efficiency

Source: Internet
Author: User
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 ( " Time consumed for sorting by LINQ: \ t {0} millisecond " , 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 ( " Time consumed for Binary sorting: \ t {0} milliseconds " , Stopmatch. elapsedmilliseconds. tostring ());
37 Console. Read ();
38
39
40 }
41
42 ///   <Summary>
43 /// Binary sorting from small to large
44 ///   </Summary>
45 ///   <Param name = "array"> String Array to be sorted </Param>
46 ///   <Param name = "start"> Start subscript of the sorting Element </Param>
47 ///   <Param name = "end"> Subscripts for sorting Elements </Param>
48 Public   Static   Void Quicksort ( String [] Array, Int Start, Int End)
49 {
50
51 // It may cause Start> end because J + 1 is used for recursive calls, and J may be 1 larger than end. This also happens if the array is empty or the input is incorrect.
52 If (End <= Start)
53 {
54 Return ;
55 }
56 Else
57 {
58
59 // Take the middle element as the center point and move it to the rightmost side.
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 // Elements less than are interchangeable with tags. equal elements cannot be exchanged. Otherwise, an endless loop is formed.
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 // Swaps the positions of the marked element with the first> = element, so that the array is divided into two parts. One part is smaller than the center value, and the other part is greater than the center value.
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

 

Test results:

Time-consuming for sorting by LINQ: 2131 milliseconds
Time consumed by binary sorting: 2083 milliseconds

Almost the same

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.