【问题标题】:Converting Selection Sort into Generics <T>, T将选择排序转换为泛型 <T>, T
【发布时间】:2017-03-31 15:41:09
【问题描述】:

我很难理解如何将选择排序转换为泛型。我写了一个经典的选择排序算法,请你帮我理解&lt;T&gt;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


    【解决方案1】:

    基本上,您要做的就是先更改签名。由于您不再希望传入int 数组而是泛型数组,因此您需要将参数类型更改为T[]。为此,您还需要通过在其中添加类型参数来使该方法具有通用性:

    public static void SelectionSort<T>(T[] numArray)
    

    由于元素类型现在是T 而不是int,因此您需要将所有引用元素ints 替换为T。例如:

    // minElement is an element of the array, so its type is T
    T minElement = numArray[i];
    
    // but the location is an index, so that stays an int
    int minLocation = i;
    

    完成此操作后,您将遇到无法在T 上使用&lt; 运算符的编译器问题。这是因为没有关于类型 T 的信息表明它有订单。所以我们在这里所做的是在T 上使用泛型类型约束。我们这里使用IComparable&lt;T&gt;接口;使用它将方法签名更改为:

    public static void SelectionSort<T>(T[] numArray)
        where T: IComparable<T>
    

    一旦我们有了它,我们就可以将 &lt; 的比较替换为对 CompaterTo 的调用:

    if (numArray[j].CompareTo(minElement) < 0)
    

    这就是这个方法要做的所有事情。这是完全转换的代码:

    public static void SelectionSort<T>(T[] numArray)
        where T: IComparable<T>
    {
       for (int i = 0; i < numArray.Length -1; ++i)
       {
           T minElement = numArray[i];
           int minLocation = i;
    
           for (int j = i + 1; j < numArray.Length; ++j)
           {
               if (numArray[j].CompareTo(minElement) < 0)
               {
                   minElement = numArray[j];
                   minLocation = j;
               }
           }
    
           if (minLocation != i)
           {
               T temp = numArray[minLocation];
               numArray[minLocation] = numArray[i];
               numArray[i] = temp;
           }
       }
    }
    

    【讨论】:

    • 非常感谢!我不太清楚在哪里放置 CompareTo() 并且我在使用 Swap 时遇到了困难 - 我没有放置 T temp。非常感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多