public class SortAlgorithm {
public static void InsertSort<T, C>(T[] array, C comparer)
where C:IComparer<T>
{
for (int i = 1; i <= array.Length - 1; i++) {
int j = i;
while (j>=1 && comparer.Compare(array[j], array[j - 1]) < 0) {
swap(ref array[j], ref array[j-1]);
j--;
}
}
}
private static void swap<T>(ref T x,ref T y) {
T temp = x;
x = y;
y = temp;
}
}
public class AlgorithmHelper {
public static void PrintArray<T>(T[] array) {
Console.Write(" Array:");
foreach (T item in array) {
Console.Write(" {0}", item);
}
Console.WriteLine();
}
}
public class ComparerFactory {
public static IComparer<int> GetIntComparer() {
return new IntComparer();
}
public class IntComparer : IComparer<int> {
public int Compare(int x, int y) {
return x.CompareTo(y);
}
}
}