Exemple #1
0
        private void AddSwappingIndexes <T>(
            IList <T> array,
            IEnumerable <MergeSortConciseModel <T> > newArray,
            int swappingIndex,
            MergeSortConciseModel <T> element)
            where T : struct,
        IComparable,
        IComparable <T>,
        IConvertible,
        IEquatable <T>,
        IFormattable
        {
            var temp = array[swappingIndex];

            array[swappingIndex]        = element.Value;
            array[element.CurrentIndex] = temp;

            this.swappingIndexes.Add(new[] { swappingIndex, element.CurrentIndex });

            var elementsToIterateThrough = newArray.Where(x => x.Value.CompareTo(temp) == 0);

            foreach (var item in elementsToIterateThrough)
            {
                item.CurrentIndex = element.CurrentIndex;
                if (elementsToIterateThrough.Count() > 1)
                {
                    element.CurrentIndex++;
                }
            }

            element.CurrentIndex = swappingIndex;
        }
Exemple #2
0
 private static void CopyRemainingElementsFromTheRight <T>(
     IReadOnlyList <T> array,
     int end,
     int rightSideIndex,
     IList <MergeSortConciseModel <T> > newArray,
     int index)
     where T : struct,
 IComparable,
 IComparable <T>,
 IConvertible,
 IEquatable <T>,
 IFormattable
 {
     while (rightSideIndex <= end)
     {
         newArray[index] = new MergeSortConciseModel <T>(array[rightSideIndex], rightSideIndex);
         rightSideIndex++;
         index++;
     }
 }
Exemple #3
0
        private static int CopyRemainingElementsFromTheLeft <T>(
            IReadOnlyList <T> array,
            int midPoint,
            int leftSideIndex,
            IList <MergeSortConciseModel <T> > newArray,
            int index)
            where T : struct,
        IComparable,
        IComparable <T>,
        IConvertible,
        IEquatable <T>,
        IFormattable
        {
            while (leftSideIndex <= midPoint)
            {
                newArray[index] = new MergeSortConciseModel <T>(array[leftSideIndex], leftSideIndex);
                leftSideIndex++;
                index++;
            }

            return(index);
        }
Exemple #4
0
        private void Merge <T>(T[] array, int start, int midPoint, int end)
            where T : struct,
        IComparable,
        IComparable <T>,
        IConvertible,
        IEquatable <T>,
        IFormattable
        {
            var newArray = new MergeSortConciseModel <T> [end - start + 1];

            var leftSideIndex  = start;
            var rightSideIndex = midPoint + 1;
            var index          = 0;

            while (leftSideIndex <= midPoint &&
                   rightSideIndex <= end)
            {
                if (array[leftSideIndex].CompareTo(array[rightSideIndex]) < 0)
                {
                    newArray[index] = new MergeSortConciseModel <T>(array[leftSideIndex], leftSideIndex);
                    leftSideIndex++;
                }
                else
                {
                    newArray[index] = new MergeSortConciseModel <T>(array[rightSideIndex], rightSideIndex);
                    rightSideIndex++;
                }

                index++;
            }

            index = CopyRemainingElementsFromTheLeft(array, midPoint, leftSideIndex, newArray, index);
            CopyRemainingElementsFromTheRight(array, end, rightSideIndex, newArray, index);

            this.CopyElements(array, start, newArray);
        }