using System;
namespace int类冒泡排序
{
class Program
{
static void Sort(int[] sortArray)
{
bool isChang = true;
do
{
isChang = false;
for (int i = 0; i < sortArray.Length - 1; i++)
{
if (sortArray[i] < sortArray[i + 1])
{
int temp = sortArray[i];
sortArray[i] = sortArray[i + 1];
sortArray[i + 1] = temp;
isChang = true;
}
}
} while (isChang);
}
static void CommonSort<T>(T[] sortArray, Func<T, T, bool> compart)
{
bool isChang = true;
do
{
isChang = false;
for (int i = 0; i < sortArray.Length - 1; i++)
{
if (compart(sortArray[i], sortArray[i + 1]))
{
T temp = sortArray[i];
sortArray[i] = sortArray[i + 1];
sortArray[i + 1] = temp;
isChang = true;
}
}
} while (isChang);
}
static void Main(string[] args)
{
/*int[] array = { 31, 423, 543, 35, 24, 454,6 };
Sort(array);
foreach (int i in array)
{
Console.WriteLine(i);
}*/
/*Employee e1 = new Employee("wei", 12000);
Employee e2 = new Employee("zz", 10000);
Employee e3 = new Employee("zh", 11000);
Employee[] earray = { e1, e2, e3 };*/
//组类声明
Employee[] earray = new Employee[]
{
new Employee("wei", 12000),
new Employee("zz", 10000),
new Employee("h", 11000)
};
CommonSort<Employee>(earray, Employee.Compare);
foreach (Employee e in earray)
{
Console.WriteLine(e);
}
}
}
}
//泛型类
using System;
using System.Collections.Generic;
using System.Text;
namespace int类冒泡排序
{
class Employee
{
public override string ToString()
{
return this .Name +":"+this .Salary;
}
public string Name { get; private set; }
public int Salary { get; private set; }
public Employee(string name ,int salary)
{
this.Name = name;
this.Salary = salary;
}
public static bool Compare(Employee e1,Employee e2)
{
if (e1.Salary > e2.Salary) return true;
return false;
}
}
}