调用merge sort c#

时间:2018-05-25 01:12:32

标签: c# sorting merge mergesort

我编写了一个合并排序程序,但是我在从另一个类调用它时遇到了问题。我需要帮助。输入尺寸和最大数量后,出于某种原因,输出中出现黑屏。我相信解决方案非常简单,但我自己找不到解决方案 这是对数字进行排序的类

 class MergeSort
        {
            public int[] Sort(int[] unsortedSequence)
            {
                int[] left;
                int[] right;
                int[] result = new int[unsortedSequence.Length];
            if (unsortedSequence.Length <= 1)
                return unsortedSequence;

            int midPoint = unsortedSequence.Length / 2;
            left = new int[midPoint];
            if (unsortedSequence.Length % 2 == 0)
                right = new int[midPoint];
            else
                right = new int[midPoint + 1];
            for (int i = 0; i < midPoint; i++)
                left[i] = unsortedSequence[i];

            int x = 0;

            for (int i = midPoint; i < unsortedSequence.Length; i++)
            {
                right[x] = unsortedSequence[i];
                x++;
            }
            left = Sort(left);
            right = Sort(right);
            result = merge(left, right);
            return result;
        }

        public static int[] merge(int[] left, int[] right)
        {
            int resultLength = right.Length + left.Length;
            int[] result = new int[resultLength];
            int indexLeft = 0, indexRight = 0, indexResult = 0;
            while (indexLeft < left.Length || indexRight < right.Length)
            {
                if (indexLeft < left.Length && indexRight < right.Length)
                {
                    if (left[indexLeft] <= right[indexRight])
                    {
                        result[indexResult] = left[indexLeft];
                        indexLeft++;
                        indexResult++;
                    }
                    else
                    {
                        result[indexResult] = right[indexRight];
                        indexRight++;
                        indexResult++;
                    }
                }
                else if (indexLeft < left.Length)
                {
                    result[indexResult] = left[indexLeft];
                    indexLeft++;
                    indexResult++;
                }
                else if (indexRight < right.Length)
                {
                    result[indexResult] = right[indexRight];
                    indexRight++;
                    indexResult++;
                }
            }
            return result;
        }
    }

这是我试图调用mergesort

的类
class Program
    {
        static void Main(string[] args)
        {
            Console.Write("How Many Random Numbers Would you like to Generate : ");
            int n = Convert.ToInt32(Console.ReadLine());
            Console.Write("What is the Maximum Random Number Would you like to Generate : ");
            int max = Convert.ToInt32(Console.ReadLine());
            Console.Clear();
            int[] unsortedSequence = generateRandomSequence(n, max);
               MergeSort mergeSortEngine = new MergeSort();
            int[] mergeSortedArray = mergeSortEngine.Sort(unsortedSequence);
            Console.Write("Output for Merge Sort: \n\n");
            OutputSequence(mergeSortedArray);
            Console.WriteLine("\n\nPress Any Key to Continue...");
            Console.ReadKey();
            Console.Clear();

2 个答案:

答案 0 :(得分:2)

因为您没有提供它们,所以我编写了缺少的generateRandomSequence()OutputSequence方法来测试您的代码,但我无法重现您的问题。也许您应该将它们与您自己进行比较:

static int[] generateRandomSequence(int count, int max)
{
    Random rn = new Random();
    int[] seq = new int[count];
    for (int i = 0; i < count; ++i)
    {
        seq[i] = rn.Next(0, max + 1);
    }
    return seq;
}

static void OutputSequence(int[] array)
{
    for (int i = 0; i < array.Length; ++i)
    {
        if (i > 0)
        {
            Console.Write(", ");
        }
        Console.Write(array[i]);
    }
    Console.WriteLine();
}

使用上述方法从代码输出:

Output

答案 1 :(得分:0)

您似乎错过了generateRandomSequence(n, max);

可能就像

public static int[] generateRandomSequence(int n, int max)
        {
            var rnd = new Random();
            int[] seq = new int[n];
            for (int ctr = 0; ctr < n; ctr++)
            {
                seq[ctr] = rnd.Next(1, max + 1);
            }
            return seq;
        }

然后,在Console.Write("Output for Merge Sort: \n\n");之后的Program / Test类中,您可以使用foreach循环进行迭代以显示已排序的数组。

foreach (var item in mergeSortedArray)
                {
                    Console.WriteLine("{0}", item);
                }

                //OutputSequence(mergeSortedArray);