111,130
社区成员
发帖
与我相关
我的任务
分享//输出演示(C#)
public static void SelectionSort<T, C>(T[] array, C comparer)
where C : IComparer<T>
{
int length = array.Length;
for (int i = 0; i <= length - 2; i++) {
Console.Write("{0}: ", i+1);
int lowestIndex = i; // 最小记录的数组索引
for (int j = length - 1; j > i; j--) {
if (comparer.Compare(array[j], array[lowestIndex]) < 0)
lowestIndex = j;
}
swap(ref array[i], ref array[lowestIndex]);
AlgorithmHelper.PrintArray(array);
}
}
static void Main(string[] args) {
int[] array = {42,20,17,13,28,14,23,15};
AlgorithmHelper.PrintArray(array);
SortAlgorithm.SelectionSort
(array, ComparerFactory.GetIntComparer());
}