就地合并排序| C#

时间:2014-06-24 12:14:02

标签: c# algorithm search mergesort in-place

我已经为合并排序编写了一个程序,该程序运行良好,直到合并它没有正确合并为止

例如:

mergesort.in:     //所有数字都是一个数组,不使用临时数组进行排序

  • 2 // - >这是数组编号
  • 5 // - >这是数组(下一行)长度
  • 4 6 98 8 24
  • 8 // - >这是数组(下一行)长度
  • 12 14 89 21 4 7 9 41

mergesort.Out:

  //Output in file mergesort.Out 
  • 4 6 8 24 98
  • 4 7 9 12 14 21 41 89

我不明白如何使用mergeArray函数进行就地合并排序

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApplication1
    {
        class ReadFromFile
        {
            static void Main()
            {
                ArrayList lines = new ArrayList();

            var input = System.IO.File.ReadAllLines(@"C:\mergesort.in");

            System.Console.WriteLine("Contents of mergesort.in = ");

            foreach (string line in input)
            {
                Console.WriteLine("\t" + line);
            }
            foreach (var i in input)
            {
                if (Convert.ToInt32(i.Length) > 1)
                {
                    var counter = 0;
                    foreach (var item in input)
                    {
                        if (input[counter].Length > 1)
                        {
                            string[] t = input[counter].Split(' ');
                            foreach (string word in t)
                            {
                                lines.Add(word);
                            }
                            ReadFromFile.mergesort(t, 0, 5, lines);
                        }
                        counter++;
                    }

                    foreach (string line in lines)
                    {
                        Console.WriteLine("\t" + line);
                    }
                    break;
                }
            }

            Console.WriteLine("Press any key to exit...");
            System.Console.ReadKey();
        }

        private static void mergesort(string[] t, int p1, int p2, ArrayList lines)
        {
            if (p1 < p2)
            {
                int mid = (p2 + p1) / 2;
                ReadFromFile.mergesort(t, p1, mid, lines);
                Console.WriteLine(p1);

                ReadFromFile.mergesort(t, mid + 1, p2, lines);
                Console.WriteLine(t);
                foreach (var item in t)
                {
                    Console.WriteLine(item);
                }
                ReadFromFile.mergeArray(t, p1, mid, p2, lines);
            }
        }

        private static void mergeArray(string[] arr, int start, int mid, int end, ArrayList lines)
        {

            //I'm not understand how to work this function for in-place merge Sort

        }
    }
}

1 个答案:

答案 0 :(得分:-4)

如果你使用Linq会更容易:

foreach (var line in input)
{
    var elements = line.Split(' ');

    if (elements.Count > 1)
    {
        var t = elements
            .Select(i => Convert.ToInt32(i))
            .Distinct()
            .OrderBy(i => i)
            .Select(i => i.ToString())
            .ToArray();

        Console.WriteLine("\t" + string.Join(" ", t));
    }
}

如果要合并多个数组中的唯一值,请使用.Union