C#排序算法——选择法

  在阅读此博文前,请先阅读我的博文“C#排序算法——基类设计 “,以了解基类的结构。
  假设有一个IList<T>型的集合list(集合的元素为list[0]到list[n-1], n = list.Count)那么选择法的排序过程如下:
  第1次遍历,从list[0]开始,到list[n-1]结束,找出list[0]到list[n-1]中最大的元素(降序则为最小的元素),设为max,将max与list[n-1]互换,显然索引号n-1位置的元素已经排好了。
  第2次遍历,从list[0]开始,到list[n-2]结束,找出list[0]到list[n-2]中最大的元素(降序则为最小的元素),设为max,将max与list[n-2]互换,显然索引号n-2位置的元素也排好了。
  ...
  第i次遍历,从list[0]开始,到list[n-i]结束,找出list[0]到list[n-i]中最大的元素(降序则为最小的元素),设为max,将max与list[n-i]互换,显然索引号n-i位置的元素也排好了。
  ...
  第n-1次遍历,从list[0]开始,到list[1]结束,找出list[0]到list[1]中最大的元素(降序则为最小的元素),设为max,将max与list[1]互换,显然索引号0,1位置的元素都排好了。
  同冒泡法一样,经过n-1次排列后,所有元素的位置都确定了。好了,现在将上面的文字描述转变成代码:

  1.      public   class  SelectionSorter : Sorter
  2.     {
  3.          public   override   void  Sort<T>(IList<T> list, CompareDelegate<T> compare)
  4.         {
  5.              for  ( int  endIndex = list.Count - 1; endIndex > 0; endIndex--)    //endIndex,子集结束位置的索引
  6.             {
  7.                  int  maxIndex = 0;    //子集中最大值元素的索引号

  8.                  for  ( int  index = 0; index <= endIndex; index++)      //找到list[0]到list[endIndex]中最大的元素,将其索引号赋值给maxIndex
  9.                 {
  10.                      if  (compare(list[maxIndex], list[index]) < 0)
  11.                     {
  12.                         maxIndex = index;
  13.                     }
  14.                 }

  15.                  if  (maxIndex != endIndex)
  16.                 {
  17.                     SwapListItem(list, maxIndex, endIndex);      //如果list[endIndex]不是最大的元素,将list[endIndex]与list[maxIndex]互换
  18.                 }
  19.             }
  20.         }

  上面的代码仍然不太完善,仍然存在效率问题,假设list已经排序过,我们的代码应该最多进行一次扫描(判断list是否需要排序),而不是傻傻地进行n-1次扫描。好了,现在得到我们的最终代码:

  1. using  System;
  2. using  System.Collections.Generic;
  3. using  System.Text;

  4. namespace  CYB.DataStruct.Sorting
  5. {
  6.      /// <summary>
  7.      /// 作者 : cyb
  8.      /// 发表时间 : 2008-9-8
  9.      /// qq : 13101908
  10.      /// e-mail : hustcyb@gmail.com
  11.      /// </summary>
  12.      public   class  SelectionSorter : Sorter
  13.     {
  14.          public   override   void  Sort<T>(IList<T> list, CompareDelegate<T> compare)
  15.         {
  16.              base .Sort(list, compare);

  17.              bool  sorted =  false ;     //判断list是否已经排序好的布尔变量

  18.              for  ( int  endIndex = list.Count - 1; endIndex > 0 && !sorted; endIndex--)
  19.             {
  20.                  int  maxIndex = 0;
  21.                 sorted =  true ;       //先假定已经排好序

  22.                  for  ( int  index = 1; index <= endIndex; index++)
  23.                 {
  24.                      if  (compare(list[maxIndex], list[index]) < 0)
  25.                     {
  26.                         maxIndex = index;
  27.                     }
  28.                      else
  29.                     {
  30.                         sorted =  false ;      //这里要注意,当且仅当list[0] < list[1] < .. <list[endIndex]时,sorted为true
  31.                     }
  32.                 }

  33.                  if  (maxIndex != endIndex)
  34.                 {
  35.                     SwapListItem(list, endIndex, maxIndex);
  36.                 }
  37.             }
  38.         }
  39.     }
  40. }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值