0
我很難理解如何將選擇排序轉換爲泛型。我已經寫了一個經典的選擇排序算法,請你幫我理解插入<T>
& T
。轉換選擇排序爲泛型<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
感謝您的幫助,您可以用選擇排序算法的剩餘部分提供。
非常感謝!我無法弄清楚放置CompareTo()的位置,而且我在Swap上遇到困難 - 我沒有放置T temp。非常感謝您的幫助! –