Chinaunix首页 | 论坛 | 博客
  • 博客访问: 226548
  • 博文数量: 59
  • 博客积分: 3000
  • 博客等级: 中校
  • 技术积分: 1340
  • 用 户 组: 普通用户
  • 注册时间: 2006-02-14 09:50
文章分类

全部博文(59)

文章存档

2009年(2)

2008年(57)

我的朋友
最近访客

分类:

2008-05-27 09:52:39

using System;

namespace BubbleSorter
{
  delegate bool CompareOp(object lhs, object rhs);

  class MainEntryPoint
  {
    static void Main()
    {
      Employee [] employees =
            {
              new Employee("Bugs Bunny", 20000),
              new Employee("Elmer Fudd", 10000),
              new Employee("Daffy Duck", 25000),
              new Employee("Wiley Coyote", (decimal)1000000.38),
              new Employee("Foghorn Leghorn", 23000),
              new Employee("RoadRunner'", 50000)};
      CompareOp employeeCompareOp = new CompareOp(Employee.RhsIsGreater);
      BubbleSorter.Sort(employees, employeeCompareOp);

      for (int i=0 ; i        Console.WriteLine(employees[i].ToString());
      Console.ReadLine();
    }
  }

  class Employee // : object
  {
    private string name;
    private decimal salary;

    public Employee(string name, decimal salary)
    {
      this.name = name;
      this.salary = salary;
    }

    public override string ToString()
    {
      return string.Format(name + ", {0:C}", salary);
    }

    public static bool RhsIsGreater(object lhs, object rhs)
    {
      Employee empLhs = (Employee) lhs;
      Employee empRhs = (Employee) rhs;
      return (empRhs.salary > empLhs.salary) ? true : false;
    }
  }

  class BubbleSorter
  {
    static public void Sort(object [] sortArray, CompareOp gtMethod)
    {
      for (int i=0 ; i      {
        for (int j=i+1 ; j        {
          if (gtMethod(sortArray[j], sortArray[i]))
          {
            object temp = sortArray[i];
            sortArray[i] = sortArray[j];
            sortArray[j] = temp;
          }
        }
      }
    }
  }
}

阅读(393) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~