函数中数组参数长度的默认参数值

时间:2022-02-24 19:19:20

I made a function that quicksorts an array that is passed through the to the function but due to the function being a recursive one it takes 2 extra parameters that aid with the quick sort. after each round of the function it sets left or right as the next area to sort and so on.

我做了一个函数,它可以快速排序一个通过函数传递给函数的数组但是由于函数是递归的它需要两个额外的参数来帮助快速排序。在每一轮函数之后,它会将左或右设置为下一个要排序的区域,以此类推。

and due to these extra parameters every time i manually call the function to do a quick sort i need to add 0 and the array length as the parameters.

由于这些额外的参数,每次我手动调用函数进行快速排序时,我需要添加0和数组长度作为参数。

   Quicksort(arr,0,arr.Length);

now this seems like it can be avoided but simply adding a default value to the left parameter(0) but the right parameter would need to be the length of the array which in this case is called elements it would be:

这看起来是可以避免的,但只要在左参数(0)中添加一个默认值就可以了,但是右参数必须是数组的长度,在本例中,这个数组被称为元素,它应该是:

   public static void QuickSort<T>(T[] elements, int left = 0, int right = elements.Length) where T : IComparable

but getting the length of the elements array is not possible.

但是获取元素数组的长度是不可能的。

I thought about making a function that would simple insert 0 and the length of the array instead of me but i was wanting to find a way to do it without an extra function if it is even possible. thanks in advanced :)

我想做一个简单的函数插入0和数组的长度而不是我,但是我想找到一种方法,如果可能的话,不需要额外的函数。由于先进的:)

 public static void QuickSort<T>(T[] elements, int left, int right) where T : IComparable
    {
        int i = left, j = right;
        T pivot = elements[(left + right) / 2];

        while (i <= j)
        {
            while (elements[i].CompareTo(pivot) < 0)
                i++;

            while (elements[j].CompareTo(pivot) > 0)
                j--;

            if (i <= j)
            {
                T tmp = elements[i];
                elements[i++] = elements[j];
                elements[j--] = tmp;
            }
        }
        if (left < j)
            QuickSort(elements, left, j);

        if (i < right)
            QuickSort(elements, i, right);

    }

}

}

3 个解决方案

#1


1  

You need a method with a nice, clean, minimal signature to expose to the outside world and a private recursive method that will do the sorting.

您需要一个具有漂亮、干净、最小签名的方法来对外公开,以及一个用于进行排序的私有递归方法。

So:

所以:

// this method is exposed to the outside world.
// Nobody needs to know about "left" and "right".
// Consumers of this method just want to "QuickSort" some elements. 
public static void QuickSort<T>(T[] elements) where T : IComparable
{
    // init the recursion here and forget about it
    QuickSortInternal(elements, 0 , elements.Length);
}

// this is your recursive method
private static void QuickSortInternal<T>(T[] elements, int left, int right) where T : IComparable
{
    // your code....
}

#2


3  

Sometimes the easiest solution is the best one: add an overload.

有时最简单的解决方案是最好的:添加超负荷。

public static void QuickSort<T>(T[] elements, int left = 0) where T : IComparable
{
    QuickSort(elements, left, elements.Length);
}

public static void QuickSort<T>(T[] elements, int left, int right) where T : IComparable
{
    // code
}

Ugly solution (use a nullable type and change the null vale with the Length). null isn't part of the legal domain of right, so no big problem:

丑陋的解决方案(使用一个可空的类型并使用长度更改null vale)。零不是法律领域的权利,所以没有大问题:

public static void QuickSort<T>(T[] elements, int left = 0, int rightTemp = null) where T : IComparable
{
    int right = rightTemp ?? elements.Length;

    // code
}

Uglier solution (use -1 as the "value-to-be-replaced"). -1 (or int.MinValue) isn't part of the legal domain of right, but this solution is ugly as hell :-)

更丑的解决方案(使用-1作为“要替换的值”)。-1(或int.MinValue)不属于权利的法律范畴,但这个解决方案丑陋得像地狱:-)

public static void QuickSort<T>(T[] elements, int left = 0, int right = -1) where T : IComparable
{
    if (right == -1)
    {
        right = elements.Length;
    }

    // code
}

#3


0  

Since you are always sending through the array itself as the first parameter, you have the length anyway. Why send through the length at all, if you can just get the length of the array from the first parameter?

由于您总是将数组本身作为第一个参数发送,因此无论如何您都有长度。如果您可以从第一个参数获得数组的长度,那么为什么还要发送长度呢?

In other words, change the code a bit, so that each call to Quicksort sends through an array only. Dont send left and right at all. Split the array you get in in each call, and then send the split array to the two sub Quicksort calls, without sending left and right each time.

换句话说,稍微修改一下代码,以便每个调用Quicksort都只通过一个数组发送。不要左右派人。分割每次调用中获得的数组,然后将分割数组发送给两个子Quicksort调用,而每次都不发送左和右。

#1


1  

You need a method with a nice, clean, minimal signature to expose to the outside world and a private recursive method that will do the sorting.

您需要一个具有漂亮、干净、最小签名的方法来对外公开,以及一个用于进行排序的私有递归方法。

So:

所以:

// this method is exposed to the outside world.
// Nobody needs to know about "left" and "right".
// Consumers of this method just want to "QuickSort" some elements. 
public static void QuickSort<T>(T[] elements) where T : IComparable
{
    // init the recursion here and forget about it
    QuickSortInternal(elements, 0 , elements.Length);
}

// this is your recursive method
private static void QuickSortInternal<T>(T[] elements, int left, int right) where T : IComparable
{
    // your code....
}

#2


3  

Sometimes the easiest solution is the best one: add an overload.

有时最简单的解决方案是最好的:添加超负荷。

public static void QuickSort<T>(T[] elements, int left = 0) where T : IComparable
{
    QuickSort(elements, left, elements.Length);
}

public static void QuickSort<T>(T[] elements, int left, int right) where T : IComparable
{
    // code
}

Ugly solution (use a nullable type and change the null vale with the Length). null isn't part of the legal domain of right, so no big problem:

丑陋的解决方案(使用一个可空的类型并使用长度更改null vale)。零不是法律领域的权利,所以没有大问题:

public static void QuickSort<T>(T[] elements, int left = 0, int rightTemp = null) where T : IComparable
{
    int right = rightTemp ?? elements.Length;

    // code
}

Uglier solution (use -1 as the "value-to-be-replaced"). -1 (or int.MinValue) isn't part of the legal domain of right, but this solution is ugly as hell :-)

更丑的解决方案(使用-1作为“要替换的值”)。-1(或int.MinValue)不属于权利的法律范畴,但这个解决方案丑陋得像地狱:-)

public static void QuickSort<T>(T[] elements, int left = 0, int right = -1) where T : IComparable
{
    if (right == -1)
    {
        right = elements.Length;
    }

    // code
}

#3


0  

Since you are always sending through the array itself as the first parameter, you have the length anyway. Why send through the length at all, if you can just get the length of the array from the first parameter?

由于您总是将数组本身作为第一个参数发送,因此无论如何您都有长度。如果您可以从第一个参数获得数组的长度,那么为什么还要发送长度呢?

In other words, change the code a bit, so that each call to Quicksort sends through an array only. Dont send left and right at all. Split the array you get in in each call, and then send the split array to the two sub Quicksort calls, without sending left and right each time.

换句话说,稍微修改一下代码,以便每个调用Quicksort都只通过一个数组发送。不要左右派人。分割每次调用中获得的数组,然后将分割数组发送给两个子Quicksort调用,而每次都不发送左和右。