Some time ago because the project needs, made a class for the sorting of the array, by the way the previous learned several sorting algorithms in C # implementation. C # Some of the mechanisms to explain the algorithm is implemented. Before reading this, we need some basic understanding of C #, understand the function of the method parameter out, ref, and grasp some basic ideas of object-oriented.
1. Insert Sort
1.1. Basic ideas:
Each time the data element to be sorted is inserted into the appropriate position in the previously sorted sequence, the sequence remains in order until all the data elements to be sorted are inserted.
1.2. Sorting process:
"Sample":
[Initial keyword] [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>
/// 插入排序算法
/// </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>
/// 在内存中交换两个数字的值
/// </summary>
/// <param name="A"></param>
/// <param name="B"></param>
static void ChangeValue (ref double A ,ref double B)
{
double Temp = A ;
A = B ;
B = Temp ;
}