# Sorting Arrays
A sort class is implemented in several ways.
1 using system;
2
3 namespace datastruct
4 {
5 public class Sorter
6 {
7/*** // <summary>
8 // bubble sort method 1
9 /// </Summary>
10 /// <Param name = "list"> </param>
11 public static void bubblesort (INT [] list)
12 {
13 For (INT I = 0; I <list. length; I ++)
14 {
15 For (Int J = I; j <list. length; j ++)
16 {
17 if (list [I] <list [J])
18 {
19 int temp = list [I];
20 list [I] = list [J];
21 list [J] = temp;
22}
23}
24}
25}
26
27/*** // <summary>
28 // insert sorting method
29 /// </Summary>
30 /// <Param name = "list"> </param>
31 public static void insertionsort (INT [] list)
32 {
33 for (INT I = 1; I <list. length; I ++)
34 {
35 int T = list [I];
36 Int J = I;
37 while (j> 0) & (list [J-1]> T ))
38 {
39 list [J] = list [J-1];
40 -- J;
41}
42 list [J] = T;
43}
44
45}
46
47/*** // <summary>
48 // select the sorting method
49 // </Summary>
50 /// <Param name = "list"> </param>
51 public static void selectionsort (INT [] list)
52 {
53 int min;
54 For (INT I = 0; I <list. Length-1; I ++)
55 {
56 min = I;
57 for (Int J = I + 1; j <list. length; j ++)
58 {
59 If (list [J] <list [Min])
60 min = J;
61}
62 int T = list [Min];
63 list [Min] = list [I];
64 list [I] = T;
65}
66
67}
68
69/** // <summary>
70 // Hill sorting method
71 /// </Summary>
72 /// <Param name = "list"> </param>
73 public static void shellsort (INT [] list)
74 {
75 int Inc;
76 for (INC = 1; INC <= List. Length/9; Inc = 3 * Inc + 1 );
77 for (; INC> 0; INC/= 3)
78 {
79 for (INT I = inc + 1; I <= List. length; I + = Inc)
80 {
81 int T = list [I-1];
82 Int J = I;
83 while (j> Inc) & (list [J-inc-1]> T ))
84 {
85 list [J-1] = list [J-inc-1];
86 J-= Inc;
87}
88 list [J-1] = T;
89}
90}
91}
92
93 Private Static void swap (ref int L, ref int R)
94 {
95 int S;
96 S = L;
97 L = R;
98 R = s;
99}
100
101/*** // <summary>
102 // quick sorting
103 /// </Summary>
104 /// <Param name = "list"> </param>
105 /// <Param name = "low"> </param>
106 /// <Param name = "high"> </param>
107 Public static void sort (INT [] list, int low, int high)
108 {
109 int second;
110 int L, R;
111 int mid;
112 if (high <= low)
113 return;
114 else if (high = low + 1)
115 {
116 If (list [low]> list [High])
117 swap (Ref List [low], Ref List [High]);
118 return;
119}
120 mid = (low + high)> 1;
121 bytes = list [Mid];
122 swap (Ref List [low], Ref List [Mid]);
123 L = low + 1;
124 r = high;
125 do
126 {
127 while (L <= R & list [l] <strong)
128 l ++;
129 while (list [R]> = tables)
130 r --;
131 If (L <R)
132 swap (Ref List [L], Ref List [R]);
133} while (L <R );
134 list [low] = list [R];
135 list [R] = comment;
136 If (low + 1 <R)
137 sort (list, low, R-1 );
138 If (R + 1 139 sort (list, R + 1, high );
140}
141}
142}
143
C # object array sorting method
Sorting is one of the commonly used methods in programming. There are many sorting methods. The following describes a simple and effective sorting method, Code As follows:
Private bool isreverse = false;
Private void sort (personalnotifentity [] list, string key)
{
If (isreverse)
{
Array. Reverse (list );
Isreverse = false;
}
Else
{
Int Len = List. length;
Type type = typeof (personalnotificationentity );
Object [] keys = new object [Len];
For (INT I = 0; I <Len; I ++)
{
Keys [I] = type. invokemember (Key, bindingflags. getfield, null, list [I], null );
}
Array. Sort (keys, list );
Isreverse = true;
}
}
The array. Sort () and array. Reverse () methods are used to sort and reverse the data. The isreverse variable is used as the marker of reverse sorting.
Two parameters are passed in. One is the list of objects to be sorted, and the other is the key of the sort keyword, that is, the object is sorted Based on the attribute or field (the value is equal to the attribute/field name of the object)
The type. invokemember () method can obtain the property/Field Value of the object instance. The field is used here.
After obtaining the value of each field to be sorted in the array, use this field value array as an array. when the parameters of the sort () method are passed in, the sort method sorts the number of objects by the value of this field.
Reprinted