C#——利用委托实现对某对象进行排序,要求实现冒泡排序和选择排序,以Car类型对象为例,(两种方法)
利用委托实现对某对象进行排序,要求实现冒泡排序和选择排序,以Car类型对象为例,其中Car类包括两个字段,name和price,最后在main方法中实现用冒泡排序和选择排序对Car对象的名称和价格进行排序。
第一种方法
1编写代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 委托排序
{
public class Car//Car类
{
public string name;
public double price;
public Car(string name,double price)
{
this.name = name;
this.price = price;
}
public static void nameSort(List<Car> x)//根据名称冒泡排序
{
for (int i = 0; i < x.Count - 1; i++)
{
Car temp;
for (int j = 0; j < x.Count - 1; j++)
{
if (string.Compare(x[j].name,x[j + 1].name)>0)
{
temp = x[j + 1];
x[j + 1] = x[j];
x[j] = temp;
}
}
}
}
public static void priceSort(List<Car> x)//根据价格冒泡排序
{
for (int i = 0; i < x.Count - 1;i++ )
{
Car temp;
for (int j = 0; j < x.Count - 1;j++ )
{
if (x[j].price > x[j + 1].price)
{
temp=x[j+1];
x[j + 1] = x[j];
x[j] = temp;
}
}
}
}
public static void nameSelectSort(List<Car> x)//根据名称选择排序
{
for (int i = 0; i < x.Count - 1; i++)
{
Car temp;
int pos = 0;
for (int j = i+1; j < x.Count; j++)
{
if (string.Compare(x[j].name, x[i].name) > 0)
{
pos = j;
}
if (pos != 0)
{
temp = x[pos];
x[pos] = x[i];
x[i] = temp;
}
}
}
}
public static void priceSelectSort(List<Car> x)//根据价格选择排序
{
for (int i = 0; i < x.Count - 1; i++)
{
Car temp;
int pos = 0;
for (int j = i+1; j < x.Count; j++)
{
if (x[j].price < x[i].price)
{
pos = j;
}
if(pos!=0)
{
temp = x[pos];
x[pos] = x[i];
x[i] = temp;
}
}
}
}
}
class Program
{
public delegate void Sorts(List<Car> x);//声明委托类型Sorts
static void Main(string[] args)
{
Car car1=new Car ("Benz",50);
Car car2=new Car ("AMW",70);
Car car3=new Car ("Cuick",3 );
Car car4=new Car("Volkswagen",15);
List<Car>list=new List<Car>();
list.Add(car1);
list.Add(car2);
list.Add (car3);
list.Add (car4);
Sorts s1 = new Sorts(Car.nameSort);//关联
s1(list);//调用
Console.WriteLine("用冒泡排序根据名称排序:");
foreach (Car x in list)
{
Console.WriteLine(x.name + "的价格是:" + x.price);
}
Console.WriteLine("----------------------------------------");
Sorts s2 = new Sorts(Car.priceSort);//关联
s2(list);//调用
Console.WriteLine("用冒泡排序根据价格排序:");
foreach(Car x in list)
{
Console.WriteLine(x.name +"的价格是:"+x.price);
}
Console.WriteLine("----------------------------------------");
Sorts s3 = new Sorts(Car.nameSort);//关联
s3(list);//调用
Console.WriteLine("用选择排序根据名称排序:");
foreach (Car x in list)
{
Console.WriteLine(x.name + "的价格是:" + x.price);
}
Console.WriteLine("----------------------------------------");
Sorts s4 = new Sorts(Car.priceSort);//关联
s4(list);//调用
Console.WriteLine("用选择排序根据价格排序:");
foreach (Car x in list)
{
Console.WriteLine(x.name + "的价格是:" + x.price);
}
Console.Read();
}
}
}
2运行结果
第二种方法
1.编写代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 委托
{
public delegate bool SortDelegate(Object obj1, Object obj2);//1声明一个委托
public class Sort//2创建排序类
{
public static void BubbleSort(Object[] obj,SortDelegate sortDelegate)//冒泡排序
{
Object temp;
for(int i=0;i<obj.Length-1;i++)
{
for(int j=0;j<obj.Length-1-i;j++)
{
if(sortDelegate(obj[j],obj[j+1]))
{
temp=obj[j];
obj[j]=obj[j+1];
obj[j+1]=temp;
}
}
}
}
public static void SelectSort(Object[]obj,SortDelegate sortDelegate) //选择排序
{
Object temp;
int pos=0;
for(int i=0;i<obj.Length-1;i++)
{
pos=i;
for(int j=i+1;j<obj.Length;j++)
{
if(sortDelegate(obj[pos],obj[j]))
{
pos=j;
}
}
//第i个数与最小的数a[pos]交换
temp=obj[i];
obj[i]=obj[pos];
obj[pos]=temp;
}
}
}
//创建一个汽车类,名为Car
public class Car
{
private string name;
private decimal price;
public string Name
{
get
{
return this.name;
}
set
{
if (value != null && !"".Equals(value))
{
this.name = value;
}
else
{
this.name = "奔驰";
}
}
}
public decimal Price
{
get
{
return this.price;
}
set
{
if (value <= 0)
{
this.price = 50;
}
else
{
this.price = value;
}
}
}
public Car(string name,decimal price)
{
this.Name=name;
this.Price=price;
}
//根据车名比较汽车的方法
public static bool CompareByName(Object obj1,Object obj2)
{
return ((Car)obj1).Name.CompareTo(((Car)obj2).name)>0;
}
//根据价格比较汽车的方法
public static bool CompareByPrice(Object obj1,Object obj2)
{
return((Car)obj1).price>((Car)obj2).price;
}
public override string ToString()
{
return Name+"的价格是"+price+"万";
}
}
class Program
{
static void Main(string[] args)
{
Car[]c=new Car[]{new Car("Volkswagen",15m),new Car("Benz",50m),new Car("BMW",70m),new Car("Buick",35m)};
Console.WriteLine("用冒泡排序根据车名排序");
Sort.BubbleSort(c,Car.CompareByName);
foreach(Car temp in c)
{
Console.WriteLine(temp);
}
Console.WriteLine();
Console.WriteLine("用冒泡排序根据价格排序");
Sort.BubbleSort(c,Car.CompareByPrice);
foreach(Car temp in c)
{
Console.WriteLine(temp);
}
Console.WriteLine();
Console.WriteLine("用选择排序根据车名排序:");
Sort.SelectSort(c,Car.CompareByName);
foreach(Car temp in c)
{
Console.WriteLine(temp);
}
Console.WriteLine();
Console.WriteLine("用选择排序根据价格排序:");
Sort.SelectSort(c,Car.CompareByPrice);
foreach(Car temp in c)
{
Console.WriteLine(temp);
}
Console.WriteLine();
Console.Read();
}
}
}
2.运行结果