C# implements insertion sort
Process dismantling
Suppose there is an existing array, as follows
The basic sorting code is as follows
static void Main(string[] args)
{
int[] myArray = new int[] {
6, 4, 2, 6, 3, 9};//替换代码
BaseSort(myArray, 1);//替换代码
for (int i = 0; i < myArray.Length; i++)
{
Console.Write(myArray[i] + " ");
}
Console.WriteLine();
Console.ReadLine();
}
public static void BaseSort(int[] array, int end)
{
int endValue = array[end];
for(int i = end - 1; i >= 0; i--)
{
if(endValue < array[i])
{
array[i + 1] = array[i];
array[i] = endValue;
}
else//到达位置,不需要再进行比较
{
break;
}
}
}
- Replace the replacement code with the following code, run and analyze
int[] myArray = new int[] {
6, 4, 2, 6, 3, 9};//替换代码
BaseSort(myArray, 1);//替换代码
//将下标 1 与之前的数值进行比较,并插入
The value of subscript 1 is less than the value of subscript 0, inserted into subscript 0
- Replace the replacement code with the following code, run and analyze
int[] myArray = new int[] {
4, 6, 2, 6, 3, 9};//替换代码
BaseSort(myArray, 2);//替换代码
//将下标 2 与之前的数值进行比较,并插入
The value of subscript 2 is less than the value of subscript 1 and 0, inserted into subscript 0
- Replace the replacement code with the following code, run and analyze
int[] myArray = new int[] {
2, 4, 6, 6, 3, 9};//替换代码
BaseSort(myArray, 3);//替换代码
//将下标 3 与之前的数值进行比较,并插入
The value of subscript 3 is not less than the value of subscript 2, do not insert
- Replace the replacement code with the following code, run and analyze
int[] myArray = new int[] {
2, 4, 6, 6, 3, 9};//替换代码
BaseSort(myArray, 4);//替换代码
//将下标 4 与之前的数值进行比较,并插入
The value of subscript 4 is less than the value of subscript 3, 2, 1, inserted into subscript 1
- Replace the replacement code with the following code, run and analyze
int[] myArray = new int[] {
2, 3, 4, 6, 6, 9 };//替换代码
BaseSort(myArray, 5);//替换代码
//将下标 5 与之前的数值进行比较,并插入
The value of subscript 5 is not less than the value of subscript 4, do not insert
Algorithm implementation
- Insertion sorting starts from subscript 1 in order , and compares the value with the previously sorted value in turn
- If the value is less than the compared value, move the compared value back and insert the value into the vacant position
- When the value is greater than/equal to the value compared, no more operations are performed, and the loop is jumped out to avoid redundant comparisons
code show as below
static void Main(string[] args)
{
int[] myArray = new int[] {
6, 4, 2, 6, 3, 9 };//替换代码
InsertionSort(myArray);
for (int i = 0; i < myArray.Length; i++)
{
Console.Write(myArray[i] + " ");
}
Console.WriteLine();
Console.ReadLine();
}
//插入排序
public static void InsertionSort(int[] array)
{
for (int i = 1; i < array.Length; i++)
{
int currentValue = array[i];
for (int j = i - 1; j >= 0; j--)
{
if (currentValue < array[j])
{
array[j + 1] = array[j];
array[j] = currentValue;
}
else
{
break;
}
}
}
}
Complexity and Stability
- Optimal time complexity: The array has been sorted. Although it takes two for loops, the inside for loop is only executed once , which is equivalent to only executing one for loop
- Worst time complexity: sorting the array in reverse order requires two for loops to move the small value to the front
- Average time complexity: In general, two for loops are required
- Space complexity: need to use the currentValue variable to record the value for comparison
- Stability: After the sorting algorithm, the same numbers behind are still in the back (for example: 6)
Due to the limited energy of the author, some mistakes and omissions will inevitably appear in the article. Experts and netizens are welcome to criticize and correct me.