【问题标题】:Implementing an integer merge sort in C#在 C# 中实现整数归并排序
【发布时间】:2017-06-07 18:56:06
【问题描述】:

最近获得了一个冒泡排序算法,并决定尝试一个合并排序算法,我正在尝试从头开始编写它作为个人挑战,但我觉得我的逻辑在其中存在根本缺陷,不知道还有哪里可以寻求建议,我欢迎任何意见。我觉得 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];

                }
            }

        }

【问题讨论】:

标签: c# mergesort


【解决方案1】:

您声明了对数组的引用,但从未初始化它们。 BC 将为 null,如果您尝试对它们执行任何操作,您将获得异常。

        //  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&lt;int&gt; 代替数组。

问题是,您只会使用B 的前半部分和C 的后半部分。那是你要的吗?如果您希望B只是A 的前半部分,而C只是A 的后半部分,请执行此操作。您可以将List&lt;int&gt; 与数组几乎完全相同地使用,但您可以将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]);
            }

不过,您可以将代码简化如下:

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# 琐事,那么您的循环方法是最好的。

【讨论】:

  • 您的解决方案非常聪明,我什至没有意识到这种方法存在。不过我会坚持使用 Loop 方法
  • @Anyon 正如我所说,我认为这是明智的。但是您将需要初始化BC,我认为您很可能希望它们成为List&lt;int&gt;
【解决方案2】:

由于您尝试使用未分配的变量(数组 B 和 C),您会遇到错误。如果您需要了解数组的工作原理,请前往here。 如果您初始化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];

                    }
                }

            }
        }

【讨论】:

    【解决方案3】:

    现在我的主要问题是如何知道我需要创建多少个数组,以便所有数组都分解为大小为 1 的数组,(当原始输入未知时)我觉得这不是一个最佳实现

    【讨论】:

      猜你喜欢
      • 2012-08-15
      • 2014-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-24
      相关资源
      最近更新 更多