twowaymergesorter.cs

来自「Data Structures and Algorithms with Obj」· CS 代码 · 共 62 行

CS
62
字号
namespace Opus6
{
    using System;

    [Version("$Id: TwoWayMergeSorter.cs,v 1.3 2001/09/11 12:04:04 brpreiss Exp $"), Copyright("Copyright (c) 2001 by Bruno R. Preiss, P.Eng.")]
    public class TwoWayMergeSorter : AbstractSorter
    {
        public static void Main()
        {
            AbstractSorter.TestSorter(new TwoWayMergeSorter(), 0x2710, 0x7b);
        }

        protected void Merge(int left, int middle, int right)
        {
            int num1 = left;
            int num2 = left;
            int num3 = middle + 1;
            while ((num2 <= middle) && (num3 <= right))
            {
                if (base.array[num2] < base.array[num3])
                {
                    this.tempArray[num1++] = base.array[num2++];
                }
                else
                {
                    this.tempArray[num1++] = base.array[num3++];
                }
            }
            while (num2 <= middle)
            {
                this.tempArray[num1++] = base.array[num2++];
            }
            for (num1 = left; num1 < num3; num1++)
            {
                base.array[num1] = this.tempArray[num1];
            }
        }

        protected override void Sort()
        {
            this.tempArray = new ComparableObject[base.n];
            this.Sort(0, base.n - 1);
            this.tempArray = null;
        }

        protected void Sort(int left, int right)
        {
            if (left < right)
            {
                int num1 = (left + right) / 2;
                this.Sort(left, num1);
                this.Sort(num1 + 1, right);
                this.Merge(left, num1, right);
            }
        }


        private ComparableObject[] tempArray;
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?