1 using System;
 2 
 3 namespace ConsoleApp1
 4 {
 5     class Program
 6     {
 7         static int[] InsertArray(int[] bornArray) 
 8         {
 9             for (int i = 0; i < bornArray.Length; i++)
10             {
11                 int temp = bornArray[i];   //记录要插入的值
12                 int j = i;  //记录当前索引
13                 while (j > 0 && (bornArray[j-1] > temp))    
14                 {
15                     bornArray[j] = bornArray[j -1];     //当前面有值大于当前值,则前面的值往后移一位
16                     j--;
17                 }
18                 bornArray[j] = temp;    //把插入的值赋值给插入的索引位置
19             }
20             return bornArray;
21         }
22         static void Main(string[] args)
23         {
24             int[] arrayInt = new int[] { 62,9,55,7,15,33};
25             Console.WriteLine("原数组为:");
26             foreach (int i in arrayInt)
27             {
28                 Console.Write(i + " ");
29             }
30             Console.WriteLine();
31             arrayInt = InsertArray(arrayInt);
32             Console.WriteLine("直接插入排序后:");
33             foreach (int i in arrayInt)
34             {
35                 Console.Write(i + " ");
36             }
37             Console.WriteLine();
38         }
39     }
40 } 

 

posted on 2020-06-08 20:22  kyuusan  阅读(393)  评论(0)    收藏  举报