首页 > 编程 > C# > 正文

用C#实现选择法排序

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

导读:本文给出了使用C#实现选择发排序的算法

using System;
namespace SelectionSorter
{
            public class SelectionSorter 
           {
                      private int min;
                     public void Sort(int [] list)
                    {
                             for(int i = 0; i < list.Length - 1; i ++)
                            { 
                                 min=i;
                                 for(int j = i + 1; j < list.Length; j ++)
                                 {
                                          if(list[j] < list[min])
                                                    min = j;
                                  } 
                                  int t=list[min];
                                  list[min]=list[i];
                                  list[i]=t;
                            }
                       } 
         } 


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

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