SoFunction
Updated on 2025-03-06

C# implements an example of insertion sorting algorithm

This article describes the method of implementing the insertion sorting algorithm in C#. Share it for your reference. The specific analysis is as follows:

The logic of this algorithm is as follows:

1. The first element can be regarded as a sorted small array. The second element is compared with this small array, placed in the appropriate position to form a new sorted number of groups.

2. The third element is compared with the new small array composed of the previous one, and determines where to rank, and loops until it ends.

public void Sort(int[] data)
{
  insertOnSort(data,1);
}
private void insertOnSort(int[] data, int index)
{
  if (index < )
  {
 int t=data[index];
 for (int i = index - 1; i >= 0; i--)
 {
   if (data[i] > t)
   {
 data[i + 1] = data[i];
 data[i] = t;
   }
   else
   {
 data[i + 1] = t;
 break;
   }
 }
 insertOnSort(data, index + 1);
  }
}

I hope this article will be helpful to everyone's C# programming.