Ejemplo n.º 1
0
        public static int[] MergeSort(int[] source, IndexData sourceIndices, Func <int, int, bool> comparator)
        {
            //Trace.WriteLine(string.Format("{0} {1}", sourceIndices.StartIndex, sourceIndices.EndIndex));

            if (sourceIndices.StartIndex == sourceIndices.EndIndex)
            {
                var result = new int[1];
                result[0] = source[sourceIndices.StartIndex];
                return(result);
            }

            IndexData leftIndices = new IndexData();

            leftIndices.StartIndex = sourceIndices.StartIndex;
            leftIndices.EndIndex   = (sourceIndices.StartIndex + sourceIndices.EndIndex) / 2;
            IndexData rightIndices = new IndexData();

            rightIndices.StartIndex = leftIndices.EndIndex + 1;
            rightIndices.EndIndex   = sourceIndices.EndIndex;

            var leftArray  = MergeSort(source, leftIndices, comparator);
            var rightArray = MergeSort(source, rightIndices, comparator);

            return(Merge(leftArray, rightArray, comparator));
        }
Ejemplo n.º 2
0
        public static int[] MergeSort(int[] input, Func <int, int, bool> comparator)
        {
            IndexData indices = new IndexData();

            indices.StartIndex = 0;
            indices.EndIndex   = input.Length - 1;
            return(MergeSort(input, indices, comparator));
        }