排序算法
排序就是将一组数据按照某种逻辑顺序重新排序的过程。排序算法的目的就是将所有元素的主键按照这种逻辑顺序排列。
先编写一个排序算法的基类,后面其他算法可以直接继承:
public class BaseSort { public BaseSort() { } public static void Sort(IComparable[] a) { } /// <summary> /// 比较 /// </summary> /// <param name="v"></param> /// <param name="w"></param> /// <returns></returns> public static bool Less(IComparable v, IComparable w) { return v.CompareTo(w) < 0; } /// <summary> /// 交换 /// </summary> /// <param name="a"></param> /// <param name="i"></param> /// <param name="j"></param> public static void Exch(IComparable[] a, int i, int j) { IComparable t = a[i]; a[i] = a[j]; a[j] = t; } public static void Show(IComparable[] a) { for (var i = 0; i < a.Length; i++) Console.WriteLine(a[i]); } }