【发布时间】:2017-03-31 15:41:09
【问题描述】:
我很难理解如何将选择排序转换为泛型。我写了一个经典的选择排序算法,请你帮我理解<T>和T的插入。
class Program
{
static void Main(string[] args)
{
int[] numbers = { 34, 17, 23, 35, 26, 9, 13 };
//Print Array in Selection Sort
SelectionSort(numbers);
for (int i = 0; i < numbers.Length; ++i)
{
Console.WriteLine(numbers[i] + " ");
}
Console.ReadLine();
}
public static void SelectionSort(int [] numArray)
{
for (int i = 0; i < numArray.Length -1; ++i)
{
int minElement = numArray[i]; //Hold smallest remaining int @ i = 0
int minLocation = i;
for (int j = i + 1; j < numArray.Length; ++j)
{
if (numArray[j] < minElement)
{
minElement = numArray[j]; // Update index of minElement
minLocation = j;
}
}
//Swap
if (minLocation != i)
{
int temp = numArray[minLocation];
numArray[minLocation] = numArray[i];
numArray[i] = temp;
}
}
}
}
据我的阅读理解,我只能做到:
public static void SelectionSort<T>(T[] numArray) : IComparable
感谢您在选择排序算法的其余部分提供的任何帮助。
【问题讨论】:
标签: c# selection-sort