static void Stabilize <T>(T[] data, BaseSort sort) where T : IComparable <T>
        {
            Wrapper <T>[] items = new Wrapper <T> [data.Length];
            for (int i = 0; i < data.Length; i++)
            {
                items[i] = new Wrapper <T>(i, data[i]);
            }

            sort.Sort(items);

            Console.Write("Index:\t");
            for (int i = 0; i < items.Length; i++)
            {
                Console.Write(items[i].Index + " ");
            }
            Console.WriteLine();
            Console.Write("Elem:\t");
            for (int i = 0; i < items.Length; i++)
            {
                Console.Write(items[i].Key + " ");
            }
            Console.WriteLine();
            Console.WriteLine();

            int index = 0;

            while (index < items.Length - 1)
            {
                while (index < items.Length - 1 &&
                       items[index].Key.Equals(items[index + 1].Key))
                {
                    // 插入排序
                    for (int j = index + 1; j > 0 && items[j].Index < items[j - 1].Index; j--)
                    {
                        if (!items[j].Key.Equals(items[j - 1].Key))
                        {
                            break;
                        }
                        Wrapper <T> temp = items[j];
                        items[j]     = items[j - 1];
                        items[j - 1] = temp;
                    }
                    index++;
                }
                index++;
            }

            Console.Write("Index:\t");
            for (int i = 0; i < items.Length; i++)
            {
                Console.Write(items[i].Index + " ");
            }
            Console.WriteLine();
            Console.Write("Elem:\t");
            for (int i = 0; i < items.Length; i++)
            {
                Console.Write(items[i].Key + " ");
            }
            Console.WriteLine();
        }
    /// <summary>
    /// 对相应排序算法执行一次耗时测试。
    /// </summary>
    /// <param name="sort">用于测试的排序算法。</param>
    /// <param name="a">用于测试的数据。</param>
    /// <returns>排序的耗时,单位为毫秒。</returns>
    public static double Time <T>(BaseSort sort, T[] a) where T : IComparable <T>
    {
        var stopwatch = Stopwatch.StartNew();

        sort.Sort(a);
        return(stopwatch.ElapsedMilliseconds);
    }
        static bool CheckStability <T>(T[] data, BaseSort sort) where T : IComparable <T>
        {
            var items = new Wrapper <T> [data.Length];

            for (var i = 0; i < data.Length; i++)
            {
                items[i] = new Wrapper <T>(i, data[i]);
            }
            sort.Sort(items);
            var index = 0;

            while (index < data.Length - 1)
            {
                while (index < data.Length - 1 && items[index].Key.Equals(items[index + 1].Key))
                {
                    if (items[index].Index > items[index + 1].Index)
                    {
                        return(false);
                    }
                    index++;
                }
                index++;
            }
            return(true);
        }
예제 #4
0
        public void Sort_SortsDatesArray()
        {
            var elements = new DateTime[] { new DateTime(2020, 02, 01), new DateTime(2019, 06, 01), new DateTime(2004, 07, 01) };

            baseSortDates.Sort(elements);

            Assert.Equal(new DateTime[] { new DateTime(2004, 07, 01), new DateTime(2019, 06, 01), new DateTime(2020, 02, 01) }, elements);
        }
예제 #5
0
 static void Test(BaseSort sort, int n, int constant)
 {
     Item <int>[] array = new Item <int> [n];
     for (int i = 0; i < n; i++)
     {
         array[i] = new Item <int>(i, constant);
     }
     sort.Sort(array);
     for (int i = 0; i < n; i++)
     {
         Console.Write(array[i].Index + " ");
     }
     Console.WriteLine();
 }
예제 #6
0
        public void BaseSortTest()
        {
            // arrange
            var baseSort = new BaseSort <int>(Items);

            // act
            baseSort.Sort();

            // assert
            for (int i = 0; i < Items.Count; i++)
            {
                Assert.AreEqual(Sorted[i], baseSort.Items[i]);
            }
        }
예제 #7
0
 public void ContextInterface(long[] inputArray)
 {
     _strategy.Sort(inputArray);
 }