namespace Inster_1
{
class Program
{
static void Main(string[] args)
{
int[] array = {3,1,2,5,6,77,88,44,21 };
foreach (var item in array)
{
Console.WriteLine(item+"----------------------");
}
Inster(array);
}
static void Inster(int[]array)
{
var len = array.Length; //原始数组 {3,1,2,5,6,77,88,44,21 };
for (var i = 1; i < len; i++)//i=1 j=0 循环一次 {1,3,2,5,6,77,88,44,21 };
{ //i=2 j=1 循环两次 {1,2,3,5,6,77,88,44,21 };
//i=3 j=2 循环三次 {1,2,3,5,6,77,88,44,21 };
for (var j = i-1; j >=0; j--) //依次从左向右边所有的数进行比较然后插入
{
if (array[j]>array[j+1])
{
var temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
for (int i = 0; i < len; i++)
{
Console.WriteLine(array[i]);
}
Console.ReadKey();
}