public delegate int DelCompare<T>(T t1, T t2);//传入两个参数来作比较
private void Form1_Load(object sender, EventArgs e)
{
//string[] nStr = { "5000", "600", "9000000", "200", "10" };
//BubSort<string>(nStr, (string t1, string t2) => {
// return t1.Length - t2.Length;
//});
//MessageBox.Show(nStr[0].ToString());
//int[] nStr = { 5, 6, 9, 2, 1 };
//BubSort<int>(nStr, (int t1, int t2) => {
// return t1 - t2;
//});
//MessageBox.Show(nStr[0].ToString());
}
public void BubSort<T>(T[] arr, DelCompare<T> del)
{
for (int i = 0; i < arr.Length - 1; i++)
{
for (int j = 0; j < arr.Length - 1 - i; j++)
{
if (del(arr[j], arr[j + 1]) < 0)
{
T temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}