Ejemplo n.º 1
0
        private void Sort <TEntity, T>(IList <TEntity> list, int from, int to, OrderSettings <TEntity, T> orderSettings)
        {
            if (from >= to)
            {
                return;
            }

            int partition = GetPartition(list, from, to, orderSettings);

            Sort(list, from, partition - 1, orderSettings);
            Sort(list, partition + 1, to, orderSettings);
        }
Ejemplo n.º 2
0
        public void TestDesc()
        {
            int[] expected = { 13, 12, 11, 7, 6, 5 };

            int[] arr = { 12, 11, 13, 5, 6, 7 };

            var orderSettings = new OrderSettings <int, int>(OrderDirection.Desc, x => x);
            var mergeSort     = new QuickSort();

            mergeSort.Sort(arr, orderSettings);

            CollectionAssert.AreEqual(expected, arr);
        }
Ejemplo n.º 3
0
        public void TestAsc()
        {
            int[] expected = { 5, 6, 7, 11, 12, 13 };

            int[] arr = { 12, 11, 13, 5, 6, 7 };

            var orderSettings = new OrderSettings <int, int>(x => x);
            var mergeSort     = new MergeSort();

            mergeSort.Sort(arr, orderSettings);

            CollectionAssert.AreEqual(expected, arr);
        }
Ejemplo n.º 4
0
        private int GetPartition <TEntity, T>(IList <TEntity> list, int from, int to, OrderSettings <TEntity, T> orderSettings)
        {
            TEntity pivot = list[to];

            int lowestIndex = from - 1;

            for (int i = from; i < to; i++)
            {
                if (orderSettings.Direction == OrderDirection.Asc)
                {
                    if (orderSettings.Compare(pivot, list[i]) > 0)
                    {
                        lowestIndex++;
                        list.Swap(lowestIndex, i);
                    }
                }
                else
                {
                    if (orderSettings.Compare(pivot, list[i]) < 0)
                    {
                        lowestIndex++;
                        list.Swap(lowestIndex, i);
                    }
                }
            }

            lowestIndex++;
            list.Swap(lowestIndex, to);

            return(lowestIndex);
        }
Ejemplo n.º 5
0
        private static void Sort <TEntity, T>(IList <TEntity> list, int from, int middle, int to, OrderSettings <TEntity, T> orderSettings)
        {
            int firstSize  = middle - from + 1;
            int secondSize = to - middle;

            var firstArray  = new TEntity[firstSize];
            var secondArray = new TEntity[secondSize];

            for (int i = 0; i < firstSize; i++)
            {
                firstArray[i] = list[from + i];
            }

            for (int i = 0; i < secondSize; i++)
            {
                secondArray[i] = list[middle + i + 1];
            }

            int firstIndex = 0, secondIndex = 0;

            int resultIndex = from;

            while (firstIndex < firstSize && secondIndex < secondSize)
            {
                if (orderSettings.Direction == OrderDirection.Asc)
                {
                    if (orderSettings.Compare(firstArray[firstIndex], secondArray[secondIndex]) <= 0)
                    {
                        list[resultIndex] = firstArray[firstIndex];
                        firstIndex++;
                    }
                    else
                    {
                        list[resultIndex] = secondArray[secondIndex];
                        secondIndex++;
                    }
                }
                else
                {
                    if (orderSettings.Compare(firstArray[firstIndex], secondArray[secondIndex]) >= 0)
                    {
                        list[resultIndex] = firstArray[firstIndex];
                        firstIndex++;
                    }
                    else
                    {
                        list[resultIndex] = secondArray[secondIndex];
                        secondIndex++;
                    }
                }

                resultIndex++;
            }

            while (firstIndex < firstSize)
            {
                list[resultIndex++] = firstArray[firstIndex++];
            }

            while (secondIndex < secondSize)
            {
                list[resultIndex++] = secondArray[secondIndex++];
            }
        }
Ejemplo n.º 6
0
 private static void SortIterative <TEntity, T>(IList <TEntity> list, int from, int to, OrderSettings <TEntity, T> orderSettings)
 {
     for (int i = 1; i < to; i *= 2)
     {
         for (int left = 0; left < to; left += 2 * i)
         {
             int middle = i + left - 1;
             int right  = Math.Min(left + 2 * i - 1, to);
             Sort(list, left, middle, right, orderSettings);
         }
     }
 }
Ejemplo n.º 7
0
        private static void Sort <TEntity, T>(IList <TEntity> list, int from, int to, OrderSettings <TEntity, T> orderSettings)
        {
            if (from >= to)
            {
                return;
            }

            int middle = (to + from) / 2;

            Sort(list, from, middle, orderSettings);
            Sort(list, middle + 1, to, orderSettings);
            Sort(list, from, middle, to, orderSettings);
        }