在C#中实现整数合并



最近获得了泡沫排序算法的功能,并决定尝试合并排序算法,我正在尝试从头开始写这算法作为个人挑战,我觉得我的逻辑好像是然而,从根本上讲是有缺陷的,不知道其他地方要咨询,我欢迎任何意见。我觉得C#不喜欢我对子阵列的声明,它们似乎也是一个不当的解决方案

public static void MergeSort(int[] A) // WIP
    {
        if (A.Length % 2 == 0 )     //Checks if input array is even
        {
            int[] B;                //Declares sub array
            int[] C;                //Declares sub array
            for (int x = 0; x < A.Length ; x++)     //Performs an action for every item item in the array
            {
                if (x < A.Length / 2)               //selects the first half of the input array and assigns it to sub-Array B
                {
                    B[x] = A[x];
                }
                if(x > A.Length / 2)                //Selects the second half of the input array and assigns it to sub-Array C
                {
                    C[x] = A[x];
                }
            }
        }

您正在声明对数组的引用,但从未初始化它们。BC将是无效的,如果您尝试使用它们做任何事情,您将获得例外。

        //  Declares *reference* to sub array, and creates it as well.
        int[] B = new int[A.Length];
        //  Declares *reference* to sub array, and creates it as well.
        int[] C = new int[A.Length];

您可以使用List<int>而不是数组。

问题是,您只使用B的上半部分和C的后半部分。那是你要的吗?如果您希望B仅为 A的上半部分,而 C则为 Just A的后半部分,请执行此操作。您可以将List<int>几乎相同与数组使用,但是可以对其进行Add(),并且它具有Count属性,而不是Length属性:

        var B = new List<int>();
        var C = new List<int>();
        for (int x = 0; x < A.Length ; x++)     //Performs an action for every item item in the array
        {
            if (x < A.Length / 2)               //selects the first half of the input array and assigns it to sub-Array B
            {
                B.Add(A[x]);
            }
            if(x > A.Length / 2)                //Selects the second half of the input array and assigns it to sub-Array C
            {
                C.Add(A[x]);
            }
        }

另一件事:我将else放在第二个if之前,为了清楚起见,我要算出更清晰的运行时效率。

但是,如果x是等于 A.Length / 2,会发生什么?如果该情况是用代码处理的,则不用介意。

            if (x < A.Length / 2)               //selects the first half of the input array and assigns it to sub-Array B
            {
                B.Add(A[x]);
            }
            else if(x > A.Length / 2)                //Selects the second half of the input array and assigns it to sub-Array C
            {
                C.Add(A[x]);
            }

您可以,Howerver,简化该代码如下:

if (A.Length % 2 == 0)
{
    //  Take the first half and make an array out of that sequence
    var B = A.Take(A.Length / 2).ToArray();
    //  Skip the first half and make an array out of the remaining sequence
    var C = A.Skip(A.Length / 2).ToArray();
}

但是,如果您打算展示基本编程基础知识而不是C#Trivia的掌握,那么您的循环方法是最好的。

您正在遇到错误,因为您正在尝试使用未分配的变量(数组B和C(。如果您需要了解阵列的工作方式,请继续使用。如果您初始化int[B] = new int[A.Length / 2];,则您的代码将不会错误。

您的代码应该看起来像这样,只是为了工作,我没有重构任何东西,只是使其起作用的最低限度:

 public static void MergeSort(int[] A) // WIP
        {
            if (A.Length % 2 == 0)     //Checks if input array is even
            {
                int[] B = new int[A.Length / 2];                //Declares sub array
                int[] C = new int[A.Length / 2];                //Declares sub array
                for (int x = 0; x < A.Length; x++)     //Performs an action for every item item in the array
                {
                    if (x < A.Length / 2)               //selects the first half of the input array and assigns it to sub-Array B
                    {
                        B[x] = A[x];
                    }
                    if (x > A.Length / 2)                //Selects the second half of the input array and assigns it to sub-Array C
                    {
                        C[x] = A[x];
                    }
                }
            }
        }

现在我的主要问题是如何知道我需要创建多少个阵列,以使所有阵列都被分解为一个尺寸的数组(当原始输入未知时(,我觉得好像这不是一个最佳实现

最新更新