创建一个通用的排序方法 [英] creating a generic sort method

查看:80
本文介绍了创建一个通用的排序方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习泛型类型,并且想要创建一个通用的QuickSort方法,问题是类不是共同变体,代码也不能编译。问题是使分区方法通用,我不知道如何去做,任何指导将不胜感激。

  public static void Swap< T>(ref T a,ref T b)
{
T temp = a;
a = b;
b = temp;


public static int Partition(int [] array,int mid)
{
int midPoint = mid,
upperBound = array.Length - 1,
lowerBound = 0;
while(lowerBound!= upperBound)
{
while(midPoint< upperBound)
{
if(array [midPoint]> array [upperBound])
{
Swap< int>(ref array [midPoint],ref array [upperBound]);
midPoint = upperBound;
休息;
}
upperBound--;
}
while(midPoint> lowerBound)
{
if(array [midPoint]< array [lowerBound])
{
Swap< int> ;(ref array [midPoint],ref array [lowerBound]);
midPoint = lowerBound;
休息;
}
lowerBound ++;
}
}
返回midPoint;

$ b $ public static void QuickSort(int [] array,int lower,int upper)
{
int mid = Partition(array,(lower + upper) / 2);
$ b $ if(upper <= lower)
{
}
else
{
QuickSort(array,mid + 1,upper) ;
QuickSort(array,lower,mid - 1);
}
}


解决方案

步骤是实际使用泛型:

  void QuickSort< T>(T [] array,。 ..)

  int Partition< T>(T [] array,...)

Partition 中,从 Swap 中删除​​泛型参数。它会被编译器推断出来。



然而,为了这个工作,你需要将 T 限制为 IComparable< T>

  void QuickSort< T>(T [数组,...)其中T:IComparable< T> 

  int Partition< T>(T [] array,...)其中T:IComparable< T> 

最后,您需要用<小于和大于 code> CompareTo :

  if(array [midPoint] .CompareTo(array [lowerBound ])<0)



<$如果(array [midPoint] .CompareTo(array [lowerBound])> 0)


I am learning generic types and wanted to create a generic QuickSort method, the problem is classes are not co-variant and code cannot compile. the problem is making the Partition method generic and i have no idea how to do it, any guidance would be appreciated.

public static void Swap<T>(ref T a, ref T b)
    {
        T temp = a;
        a = b;
        b = temp;
    }

    public static int Partition(int[] array, int mid)
    {
        int midPoint = mid,
            upperBound = array.Length - 1,
            lowerBound = 0;
        while (lowerBound != upperBound)
        {
            while (midPoint < upperBound)
            {
                if (array[midPoint] > array[upperBound])
                {
                    Swap<int>(ref array[midPoint], ref array[upperBound]);
                    midPoint = upperBound;
                    break;
                }
                upperBound--;
            }
            while (midPoint > lowerBound)
            {
                if (array[midPoint] < array[lowerBound])
                {
                    Swap<int>(ref array[midPoint], ref array[lowerBound]);
                    midPoint = lowerBound;
                    break;
                }
                lowerBound++;
            }
        }
        return midPoint;
    }

    public static void QuickSort(int[] array,int lower,int upper)
    {
        int mid = Partition(array, (lower + upper) / 2);

        if (upper <= lower)
        {
        }
        else
        {
            QuickSort(array, mid + 1, upper);
            QuickSort(array, lower, mid - 1);
        }
    }

解决方案

First step is to actually use generics:

void QuickSort<T>(T[] array, ...)

and

int Partition<T>(T[] array, ...)

In Partition remove the generic argument from Swap. It will be inferred by the compiler.

However, for this to work, you need to constrain T to IComparable<T>:

void QuickSort<T>(T[] array, ...) where T : IComparable<T>

and

int Partition<T>(T[] array, ...) where T : IComparable<T>

Finally, you need to replace the "less than" and "greater than" operators with calls to CompareTo:

if(array[midPoint].CompareTo(array[lowerBound]) < 0)

and

if(array[midPoint].CompareTo(array[lowerBound]) > 0)

这篇关于创建一个通用的排序方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆