선택 정렬을 제네릭으로 변환하는 방법을 이해하는 데 어려움이 있습니다. 고전적인 선택 정렬 알고리즘을 작성했습니다. <T>
& T
의 삽입을 이해하는 데 도움이 될 수 있습니까?선택 사항을 Generics로 정렬 <T>, T
public static void SelectionSort<T>(T[] numArray) : IComparable
당신이 선택 정렬 알고리즘의 나머지 제공 할 수있는 모든 도움을 주셔서 감사합니다
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;
}
}
}
}
는 지금까지 내가 내 독서에서 이해할 수있다, 나는 단지까지로 얻을 수 있습니다.
정말 고마워요! 나는 CompareTo()를 어디에 두어야하는지 잘 알지 못했고 스왑에 어려움을 겪고있었습니다. 저는 T temp를 넣지 않았습니다. 도움을 주셔서 대단히 감사합니다! –