Пример #1
0
        /// <exception cref="System.Exception"/>
        public virtual void TestQuickSort()
        {
            QuickSort sorter = new QuickSort();

            SortRandom(sorter);
            SortSingleRecord(sorter);
            SortSequential(sorter);
            SortSorted(sorter);
            SortAllEqual(sorter);
            SortWritable(sorter);
            // test degenerate case for median-of-three partitioning
            // a_n, a_1, a_2, ..., a_{n-1}
            int Dsample = 500;

            int[] values = new int[Dsample];
            for (int i = 0; i < Dsample; ++i)
            {
                values[i] = i;
            }
            values[0] = values[Dsample - 1] + 1;
            TestIndexedSort.SampleSortable s = new TestIndexedSort.SampleSortable(values);
            values = s.GetValues();
            int Dss = (Dsample / 2) * (Dsample / 2);

            // Worst case is (N/2)^2 comparisons, not including those effecting
            // the median-of-three partitioning; impl should handle this case
            TestIndexedSort.MeasuredSortable m = new TestIndexedSort.MeasuredSortable(s, Dss);
            sorter.Sort(m, 0, Dsample);
            System.Console.Out.WriteLine("QuickSort degen cmp/swp: " + m.GetCmp() + "/" + m.GetSwp
                                             () + "(" + sorter.GetType().FullName + ")");
            Arrays.Sort(values);
            int[] check = s.GetSorted();
            Assert.True(Arrays.Equals(values, check));
        }
Пример #2
0
        /// <exception cref="System.Exception"/>
        public virtual void SortAllEqual(IndexedSorter sorter)
        {
            int Sample = 500;

            int[] values = new int[Sample];
            Arrays.Fill(values, 10);
            TestIndexedSort.SampleSortable s = new TestIndexedSort.SampleSortable(values);
            sorter.Sort(s, 0, Sample);
            int[] check = s.GetSorted();
            Assert.True(Arrays.ToString(values) + "\ndoesn't match\n" + Arrays
                        .ToString(check), Arrays.Equals(values, check));
            // Set random min/max, re-sort.
            Random r   = new Random();
            int    min = r.Next(Sample);
            int    max = (min + 1 + r.Next(Sample - 2)) % Sample;

            values[min] = 9;
            values[max] = 11;
            System.Console.Out.WriteLine("testAllEqual setting min/max at " + min + "/" + max
                                         + "(" + sorter.GetType().FullName + ")");
            s = new TestIndexedSort.SampleSortable(values);
            sorter.Sort(s, 0, Sample);
            check = s.GetSorted();
            Arrays.Sort(values);
            Assert.True(check[0] == 9);
            Assert.True(check[Sample - 1] == 11);
            Assert.True(Arrays.ToString(values) + "\ndoesn't match\n" + Arrays
                        .ToString(check), Arrays.Equals(values, check));
        }
Пример #3
0
        /// <exception cref="System.Exception"/>
        public virtual void SortSingleRecord(IndexedSorter sorter)
        {
            int Sample = 1;

            TestIndexedSort.SampleSortable s = new TestIndexedSort.SampleSortable(Sample);
            int[] values = s.GetValues();
            sorter.Sort(s, 0, Sample);
            int[] check = s.GetSorted();
            Assert.True(Arrays.ToString(values) + "\ndoesn't match\n" + Arrays
                        .ToString(check), Arrays.Equals(values, check));
        }
Пример #4
0
        /// <exception cref="System.Exception"/>
        public virtual void SortSequential(IndexedSorter sorter)
        {
            int Sample = 500;

            int[] values = new int[Sample];
            for (int i = 0; i < Sample; ++i)
            {
                values[i] = i;
            }
            TestIndexedSort.SampleSortable s = new TestIndexedSort.SampleSortable(values);
            sorter.Sort(s, 0, Sample);
            int[] check = s.GetSorted();
            Assert.True(Arrays.ToString(values) + "\ndoesn't match\n" + Arrays
                        .ToString(check), Arrays.Equals(values, check));
        }
Пример #5
0
        /// <exception cref="System.Exception"/>
        public virtual void SortRandom(IndexedSorter sorter)
        {
            int Sample = 256 * 1024;

            TestIndexedSort.SampleSortable s = new TestIndexedSort.SampleSortable(Sample);
            long seed = s.GetSeed();

            System.Console.Out.WriteLine("sortRandom seed: " + seed + "(" + sorter.GetType().
                                         FullName + ")");
            int[] values = s.GetValues();
            Arrays.Sort(values);
            sorter.Sort(s, 0, Sample);
            int[] check = s.GetSorted();
            Assert.True("seed: " + seed + "\ndoesn't match\n", Arrays.Equals
                            (values, check));
        }
Пример #6
0
        /// <exception cref="System.Exception"/>
        public virtual void SortSorted(IndexedSorter sorter)
        {
            int Sample = 500;

            int[]  values = new int[Sample];
            Random r      = new Random();
            long   seed   = r.NextLong();

            r.SetSeed(seed);
            System.Console.Out.WriteLine("testSorted seed: " + seed + "(" + sorter.GetType().
                                         FullName + ")");
            for (int i = 0; i < Sample; ++i)
            {
                values[i] = r.Next(100);
            }
            Arrays.Sort(values);
            TestIndexedSort.SampleSortable s = new TestIndexedSort.SampleSortable(values);
            sorter.Sort(s, 0, Sample);
            int[] check = s.GetSorted();
            Assert.True(Arrays.ToString(values) + "\ndoesn't match\n" + Arrays
                        .ToString(check), Arrays.Equals(values, check));
        }