Some time ago, because of the project needs, did a class used to sort the array, by the way, some of the previously learned sorting algorithms in C # implementation. Some of the mechanisms of C # to explain the implementation of the algorithm. Before reading this, you need some basic understanding of C #, understanding the role of out, ref in the method parameters, and mastering some basic ideas of object-oriented.
1. Insert Sort
1.1. Basic ideas:
Each time a data element to be sorted is inserted into the appropriate position in the previously sorted sequence, the sequence remains orderly until the data element to be sorted is fully inserted.
1.2. Sorting process:
"Example":
[Initial key Words] [49] 38 65 97 76 13 27 49
(38) [38 49] 65 97 76 13 27 49
(65) [38 49 65] 97 76 13 27 49
(97) [38 49 65 97] 76 13 27 49
(76) [38 49 65 76 97] 13 27 49
(13) [13 38 49 65 76 97] 27 49
(27) [13 27 38 49 65 76 97] 49
(49) [13 27 38 49 49 65 76 97]
1.3. Program implementation
<summary>
//Insert Sort algorithm
//</ Summary>
//<param name= "Dblarray" ></PARAM>
static void Insertsort (ref double[] Dblarray)
{
for (int i = 1; i < dblarray.length; i++)
{
int frontarrayindex = i-1;
int currentchangeindex = i;
while (frontarrayindex>=0)
{
if (Dblarray[currentchangeindex] < Dblarray[frontarrayindex])
{
ChangeValue (ref dblarray[currentchangeindex],ref Dblarray[frontarrayindex]);
Currentchangeindex = Frontarrayindex;
}
frontarrayindex--;
}
}
}
//<summary>
//Exchange two numeric values in memory
//</SUMMARY>
//<param name= "A" > </param>
//<param name= "B" ></PARAM>
static void ChangeValue (ref double A, ref double b) br>{
Double Temp = A;
A = B;
B = Temp;
} |
2. Select Sort
2.1. Basic idea:
Each trip selects the smallest (or largest) element from the data element to be sorted, placing the order at the end of the ordered sequence. Until all the data elements are sorted out.
2.2. Sorting procedure:
"Example":
Initial keywords [+ +]
First Order after [all points]
After the second trip, you'll be in the following order.
After the third trip [$]
Fourth order after a five-step trip [$ three]
V After the order of the tour [the]
The sixth trip after the Order of the seventh ([+]
) After the order of a. [+]
last row The results of the order: 2.3. Program implementation
//<summary>
//select sort
//&L T;/summary>
//<param name= "Dblarray" ></PARAM>
private static void Selectsort (ref double[] Dblarray)
{
for (int i =0; i< dblarray.length; i++)
{
Double MinValue = dblarray[i];
int minvalueindex = i;
for (int j = i; j< dblarray.length; j + +)
{
if (MinValue > Dblarray[j])
{
MinValue = Dbl ARRAY[J];
Minvalueindex = j;
}
}
Exchangevalue (ref dblarray[i], ref dblarray[minvalueindex]);
}
}
///<summary>
//Exchange data
//</SUMMARY>
//<param name= "A" ></param>
//<param name= "B" ></PARAM>
private static void Exchangevalue (ref double A, ref double b)
{ double Temp = A;
A = B;
B = Temp;
} |
3. Bubble sort
3.1. Basic ideas:
22 compares the size of the data element to be sorted, and finds that two data elements are exchanged in reverse order until there are no reversed data elements.
3.2. Sorting process:
Imagine the sorted array r[1: N] vertically erected, each data element as a weight of the bubble, according to the light bubbles can not be under the principle of heavy bubbles, from the bottom to scan the array r, where the light bubbles that violate this principle, so that they upward "floating", so repeated, until the last two bubbles are light in the top, the heavy is down so far
"Example":
49 13 13 13 13 13 13 13
38 49 27 27 27 27 27 27
65 38 49 38 38 38 38 38
97 65 38 49 49 49 49 49
76 97 65 49 49 49 49 49
13 76 97 65 65 65 65 65
27 27 76 97 76 76 76 76
49 49 49 76 97 97 97 97
3.3. Program implementation
The program supports sequential and reverse order.
<summary>
Bubbling algorithm
</summary>
<param name= "Abarray" ></param>
<param name= "isascending" > whether sequential sorting </param>
<returns></returns>
private static double[] Bubblearithmetic (double[] Abarray, BOOL isascending)
{
if (Abarray. Length > 0)
{
for (int i = Abarray. Length-1; I >=0; i--)
{
for (int j = i-1; j>=0; j--)
{
if (Checkaccordcondition (abarray[i],abarray[j],isascending))
{
Exchangevalue (ref abarray[i],ref abarray[j]);
}
}
}
}
return abarray;
}
<summary>
Exchanging data
</summary>
<param name= "A" ></param>
<param name= "B" ></param>
private static void Exchangevalue (ref double A, ref double B)
{
Double Temp = A;
A = B;
B = Temp;
}
<summary>
Whether the condition is met
</summary>
<returns></returns>
private static bool Checkaccordcondition (double data1, double data2, bool isascending)
{
if (Data1 > Data2)
{
return isascending = = true? True:false;
}
Else
{
return isascending = = true? False:true;
}
} |
4. Quick Sort
4.1. Basic ideas:
Any data element in the current unordered area R[1..h] as the "datum" of the comparison (as may be remembered as X), using this datum to divide the current unordered division into the left and right two smaller unordered areas: r[1..i-1] and r[i+1..h], and the left unordered sub-region data elements are less than or equal to the datum elements. The data elements in the unordered sub-region on the right are greater than or equal to the datum elements, while the Datum X is in the final sort position, that is, R[1..i-1]≤x.key≤r[i+1..h] (1≤i≤h), when R[1..i-1] and r[i+1..h] are not empty, Each of them is divided into the above process until all the data elements in the unordered sub-regions are sorted.
4.2. Sorting process:
"Example":
initial keyword [49 38 65 97 76 13 27 49]
After the first Exchange [27 38 65 97 76 13 49 49]
After the second exchange [27 38 49 97 76 13 65 49]
After the third Exchange [27 38 13 97 76 49 65 49]
After the fourth Exchange [27 38 13 49 76 97 65 49]
[27 38 13 49 76 97 65 49]
(One-time partitioning process)
initial keyword [49 38 65 97 76 13 27 49]
After a trip sort [27 38 13] 49 [76 97 65 49]
Two times sorted [13] 27 [38] 49 [49 65]76 [97]
Three trips sorted after 13 27 38 49 49 [65]76 97
Final sorted results 13 27 38 49 49 65 76 97
Status after the order of each trip
4.3. Program implementation
<summary>
Quick Sort Method
</summary>
<param name= "Dbarray" ></param>
<param name= "StartIndex" ></param>
<param name= "EndIndex" ></param>
private static void QuickSort (ref double[] Dbarray, int StartIndex, int EndIndex)
{
Base
int currentindex = StartIndex;
Sequential Lookup
bool isordersearched = true;
Reverse order Lookup
bool isdisordersearched = true;
while (isordersearched | | isdisordersearched)
{
Isdisordersearched = false;
Isordersearched = false;
for (int i =endindex; i>currentindex; i--)
{
if (Dbarray[i] < Dbarray[currentindex])
{
Exchangevalue (ref dbarray[i], ref dbarray[currentindex]);
Currentindex = i;
Isdisordersearched = true;
break;
}
}
for (int i = StartIndex; i < currentindex; i++)
{
if (Dbarray[i] > Dbarray[currentindex])
{
Exchangevalue (ref dbarray[i], ref dbarray[currentindex]);
Currentindex = i;
Isordersearched = true;
break;
}
}
}
if (Endindex-startindex > 0)
{
if (currentindex! = StartIndex)
{
QuickSort (ref dbarray, startindex,currentindex-1);
}
if (currentindex! = EndIndex)
{
QuickSort (ref dbarray, Currentindex+1,endindex);
}
}
}
Exchanging data
</summary>
<param name= "A" ></param>
<param name= "B" ></param>
private static void Exchangevalue (ref double A, ref double B)
{
Double Temp = A;
A = B;
B = Temp;
} |
5. Heap Sequencing
5.1. Basic ideas:
Heap sorting is a tree-shaped selection sort, in which the R[1..N] is considered as a sequential storage structure of a complete binary tree, and the smallest element is selected using the intrinsic relationship between the parent node and the child node in the complete binary tree.
5.2. Definition of the heap:
The sequence of N elements is K1,k2,k3,..., Kn. is called a heap when and only if the sequence satisfies the attribute: Ki≤k2i ki≤k2i+1 (1≤I≤[N/2]).
A heap is essentially a complete binary tree that satisfies the following properties: Any non-leaf node in the tree has a keyword that is greater than or equal to its child's node point. For example, a sequence 10,15,56,25,30,70 is a heap, which corresponds to a complete binary tree as shown. The root node in this heap (called the heap top) has the smallest keyword, which we call a small Gan. Conversely, if the keyword of any non-leaf node in a complete binary tree is greater than or equal to the key word of the child, it is called the large root heap.
5.3. Sorting process:
Heap ordering is the use of small Gan (or large root heap) to select the small (or maximum) number of keywords in the current unordered region of the record implementation sort. We may as well use the big root heap to sort. The basic operation for each trip is to adjust the current unordered area to a large heap, select the top record of the heap with the largest keyword, and swap it with the last record in the unordered area. In this way, just as opposed to direct selection, the ordered area is formed at the tail of the original record area and gradually expands to the entire record area.
Example: Build a heap on the 42,13,91,23,24,16,05,88 of the keyword sequence.
5.4. Program implementation
<summary>
Small Gan Sort
</summary>
<param name= "Dblarray" ></param>
<param name= "StartIndex" ></param>
<returns></returns>
private static void Heapsort (ref double[] Dblarray)
{
for (int i = dblarray.length-1; I >= 0; i--)
{
if (2*i+1<dblarray.length)
{
int minchildrenindex = 2*i+1;
Compare Saozi right subtree, record index of minimum value
if (2*i+2 < dblarray.length)
{
if (dblarray[2*i+1]>dblarray[2*i+2])
Minchildrenindex = 2*i+2;
}
if (Dblarray[i] > Dblarray[minchildrenindex])
{
Exchagevalue (ref dblarray[i],ref Dblarray[minchildrenindex]);
Nodesort (ref dblarray, Minchildrenindex);
}
}
}
}
<summary>
Node ordering
</summary>
<param name= "Dblarray" ></param>
<param name= "StartIndex" ></param>
private static void Nodesort (ref double[] Dblarray,int StartIndex)
{
while (2*startindex+1 < dblarray.length)
{
int minchildrenindex = 2*startindex+1;
if (2*startindex+2 < dblarray.length)
{
if (dblarray[2*startindex+1]>dblarray[2*startindex+2])
{
Minchildrenindex = 2*startindex+2;
}
}
if (Dblarray[startindex] > Dblarray[minchildrenindex])
{
Exchagevalue (ref dblarray[startindex],ref Dblarray[minchildrenindex]);
StartIndex = Minchildrenindex;
}
}
}
<summary>
Exchange value
</summary>
<param name= "A" ></param>
<param name= "B" ></param>
private static void Exchagevalue (ref double A, ref double B)
{
Double Temp = A;
A = B;
B = Temp;
} |
Summary :
People often say that the algorithm is the soul of the program, in the course of the project is often attention and not the soul out of the body. Always review the importance of previous data as if Christians were to worship every week. It is not possible to ignore the importance of the foundation because of the platform of C # and Java.
C # Common algorithms-sorting algorithm "original exclusive"