首页 > 编程语言 > 详细

C#直接插入排序

时间:2020-06-08 20:39:40      阅读:40      评论:0      收藏:0      [点我收藏+]
 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 } 

 

C#直接插入排序

原文:https://www.cnblogs.com/kyuusan/p/13068195.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!