| Код | class Program { static void Main() { var array = new int[]{5,4,3,2,1}; Console.WriteLine(String.Join(" ", new SellectionSorter<int>().Sort(array))); Console.WriteLine(String.Join(" ", new InsertSorter<int>().Sort(array))); Console.ReadKey(); } }
interface ISorter<T> where T:IComparable { T[] Sort(T[] array); }
class InsertSorter<T> : ISorter<T> where T : IComparable { public T[] Sort(T[] array) { if (array.Length < 2) return array; var result = new T[array.Length]; array.CopyTo(result, 0); for (var i = 1; i < result.Length; i++) { var key = result[i]; var j = i - 1; while (j >= 0 && result[j].CompareTo(key) == 1) { result[j + 1] = result[j]; j--; } result[j + 1] = key; } return result; } }
class SellectionSorter<T> : ISorter<T> where T : IComparable { public T[] Sort(T[] array) { var result = new T[array.Length]; array.CopyTo(result, 0); for (var i = 0; i < result.Length - 1; i++) { var min = i; for (var j = i + 1; j < result.Length; j++) { if (result[j].CompareTo(result[min]) == -1) min = j; } if (result[i].CompareTo(result[min]) != 0) { var buf = result[i]; result[i] = result[min]; result[min] = buf; } } return result; } }
|
|