结合冒泡排序学习委托和事件系列一

所谓冒泡排序就是将相邻两个数进行比较,讲较小的掉到前头。

namespace ConsoleApplication1

{

  class  委托和事件

    {

        public static void arraySort(int[] items)//传递的是整形数组

        {

            int i, j, item;

 

            for (i = 0; i <= items.Length - 1; i++)

            {

                for (j = 0; j <items.Length - 1 - i; j++)

                {

                    item = items[j];

                    items[j] = items[j + 1];

                    items[j + 1] = item;

                }

            }

            Console.WriteLine("冒泡好的数组为:");

            foreach (int index in items)

            {

                Console.Write(index);

            }

        }

    }

}

 

改方法是对一个整形数组进行降序排序,但是如果你想升序排列,有两种方案,一种是将小于元算符修改为大于,另一种是用一个参数来实现。代码实现为:

 

namespace ConsoleApplication1

{

  class  委托和事件

    {

      //定义排序类型

      public enum sorType{ Ascending, Descending }

 

      public static void arraySort(int[] items,sorType sortOrder)

      {

          int i, j, item;

          for (i = 0; i <= items.Length - 1; i++)

          {

              for (j = 1; j <= items.Length - 1 - i;j++ )

              {

                  switch(sortOrder)//判断是那种类型

                  {

                      case sorType.Ascending : //升序

                          if (items[j - 1] > items[j])

                          {

                              item = items[j - 1];

                              items[j - 1] = items[j];

                              items[j] = item;

                          }

                          {}

                          break;

                      case sorType.Descending://降序

                          if(items[j-1]<items[j])

                          {

                              item = items[j - 1];

                              items[j - 1] = items[j];

                              items[j] = item;

                          }

                          break;

                  }

              }

          }

 

          Console.WriteLine("冒泡好的数组为:");

          foreach (int index in items)

          {

              Console.Write(index);

          }

 

 

      }

       

    }

}

 

然而,上述代码只是照顾了两种可能的排序顺序,假如要实现按字母,按随即顺序,这种方法就会变得非常恐怖。下面是委托排上用场的时候

 

namespace ConsoleApplication1
{
  class  委托和事件
    {
      //定义委托
      public  delegate bool delegateSort(int first,int second);
      //定义方法,实现顺序排序
      public static bool functionSort(int first,int second)
      {
          return(first>second);
      }
      //定义方法,实现倒序排序

      public static bool functionDescSort(int first ,int second)
      {
          return(first<second);
      }

      //定义方法,实现按字母排序
      public static bool functionAlpha(int first,int second)
      {
          int comparison;
          comparison=(first.ToString().CompareTo(second.ToString()));
          return(comparison>0);
      }

    

      public static void Oreder(int[] items, delegateSort funciontSort)//让委托指向方法
      {
          int i, j, m;
          for (i = 0; i <= items.Length - 1; i++)
          {
              for (j = 0; j < items.Length - 1 - i; j++)
              {
                  if (funciontSort(items[j], items[j + 1]))
                  {
                      m = items[j];
                      items[j] = items[j + 1];
                      items[j+1] = m;
                  }
              }
          }
          Console.WriteLine("排序好的数据");
          foreach(int indxe in items)
          {
              Console.Write(indxe);
          }
      }

    }

}

可以看出,和前面的方法相比较,现在添加一个附加的排序机制是多么的简单。

 

 

 
原文地址:https://www.cnblogs.com/xinyebs/p/2435463.html