首页 > 编程 > C# > 正文

用C#实现插入排序

2023-05-16 12:36:07
字体:
来源:转载
供稿:网友

导读:本文介绍了使用C#实现插入法排序的算法

using System;
namespace InsertionSorter
{
       public class InsertionSorter 
       {
              public void Sort(int [] list)
              {
                       for(int i = 1; i < list.Length; i ++)
                      {
                                int t = list[i];
                                int j = i;
                                while(( j > 0)&&(list[j - 1] > t))
                                {
                                          list[j] = list[j - 1];
                                          -- j;
                                } 
                                list[j] = t;
                      } 
               } 
       }
 

       public class MainClass 
        {
                  public static void Main()
                  { 
                            int[] iArrary = new int[]{1,13,3,6,10,55,98,2,87,12,34,75,33,47};
                            InsertionSorter ii = new InsertionSorter();
                             ii.Sort(iArrary)
                            for(int m = 0;m < iArrary.Length;m ++)
                                  Console.Write("{0}",iArrary[m]);
                            Console.WriteLine();
                 }
         }
}

上一篇:C#快速排序

下一篇:用C#实现冒泡法排序

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表