Beispiel #1
0
        public void insertion_sort_success()
        {
            for (int n = 1; n < 100; n += 10)
            {
                for (int i = 0; i < 100; i++)
                {
                    int[]    random   = UniformDiscreteDistribution.Random(n);
                    double[] expected = random.ToDouble();
                    double[] a        = random.ToDouble();
                    double[] b        = random.ToDouble();
                    double[] c        = random.ToDouble();
                    double[] d        = random.ToDouble();
                    double[] e        = random.ToDouble();

                    Array.Sort(expected);
                    Sort.Insertion(a);
                    Sort.Insertion(b, asc: false);
                    b = b.Reversed();
                    Sort.Insertion(c, asc: true);

                    Func <double, double, int> greater = (x, y) => - x.CompareTo(y);
                    Sort.Insertion(d, comparer: greater, asc: false);
                    Sort.Insertion(e, comparer: greater, asc: true);
                    e = e.Reversed();

                    Assert.IsTrue(expected.IsEqual(a));
                    Assert.IsTrue(expected.IsEqual(b));
                    Assert.IsTrue(expected.IsEqual(c));
                    Assert.IsTrue(expected.IsEqual(d));
                    Assert.IsTrue(expected.IsEqual(e));
                }
            }
        }
Beispiel #2
0
        public void bottom_1()
        {
            Accord.Math.Random.Generator.Seed = 0;

            for (int size = 1; size < 1000; size *= 10)
            {
                for (int n = 1; n < size; n += 10)
                {
                    for (int i = 0; i < 100; i++)
                    {
                        int[] random = UniformDiscreteDistribution.Random(size);
                        int[] sorted = random.Sorted();

                        int[] expectedBottom = sorted.First(n);
                        int[] bottomIdx      = random.Bottom(n);
                        int[] actualBottom   = random.Submatrix(bottomIdx);
                        Assert.IsTrue(actualBottom.IsEqual(expectedBottom));

                        int[] expectedTop = sorted.Last(n).Reversed();
                        int[] topIdx      = random.Top(n);
                        int[] actualTop   = random.Submatrix(topIdx);
                        Assert.IsTrue(actualTop.IsEqual(expectedTop));
                    }
                }
            }
        }
Beispiel #3
0
        public void insertion_sort_with_items_success()
        {
            for (int n = 1; n < 100; n += 10)
            {
                for (int i = 0; i < 100; i++)
                {
                    int[]    random   = UniformDiscreteDistribution.Random(n);
                    double[] expected = random.ToDouble();
                    double[] a        = random.ToDouble();
                    long[]   ai       = random.ToInt64();
                    double[] b        = random.ToDouble();
                    long[]   bi       = random.ToInt64();
                    double[] c        = random.ToDouble();
                    long[]   ci       = random.ToInt64();
                    double[] d        = random.ToDouble();
                    long[]   di       = random.ToInt64();
                    double[] e        = random.ToDouble();
                    long[]   ei       = random.ToInt64();

                    Array.Sort(expected);
                    Sort.Insertion(a, ai);
                    Sort.Insertion(b, bi, asc: false);
                    b  = b.Reversed();
                    bi = bi.Reversed();
                    Sort.Insertion(c, ci, asc: true);

                    Func <double, double, int> greater = (x, y) => - x.CompareTo(y);
                    Sort.Insertion(d, di, comparer: greater, asc: false);
                    Sort.Insertion(e, ei, comparer: greater, asc: true);
                    e  = e.Reversed();
                    ei = ei.Reversed();

                    Assert.IsTrue(a.IsSorted());
                    Assert.IsTrue(a.IsSorted(ComparerDirection.Ascending));
                    Assert.IsTrue(expected.IsEqual(a));
                    Assert.IsTrue(expected.IsEqual(b));
                    Assert.IsTrue(expected.IsEqual(c));
                    Assert.IsTrue(expected.IsEqual(d));
                    Assert.IsTrue(expected.IsEqual(e));

                    Assert.IsTrue(expected.IsEqual(ai));
                    Assert.IsTrue(expected.IsEqual(bi));
                    Assert.IsTrue(expected.IsEqual(ci));
                    Assert.IsTrue(expected.IsEqual(di));
                    Assert.IsTrue(expected.IsEqual(ei));
                }
            }
        }
Beispiel #4
0
        public void nth_element_1()
        {
            for (int size = 1; size < 1000; size += 10)
            {
                for (int nth = 1; nth < size; nth += 10)
                {
                    int[]    random = UniformDiscreteDistribution.Random(size);
                    double[] a      = random.ToDouble();
                    double[] b      = random.ToDouble();
                    int[]    bk     = (int[])random.Clone();
                    double[] c      = random.ToDouble();
                    double[] d      = random.ToDouble();
                    int[]    dk     = (int[])random.Clone();
                    double[] e      = random.ToDouble();
                    double[] f      = random.ToDouble();
                    int[]    fk     = (int[])random.Clone();

                    Sort.NthElement(a, 0, a.Length - 1, nth);
                    Sort.NthElement(b, bk, 0, b.Length - 1, nth);
                    Assert.IsTrue(a.IsEqual(b));
                    Assert.IsTrue(a.IsEqual(bk));

                    Func <double, double, int> comparer = (x, y) => x.CompareTo(y);
                    Sort.NthElement(c, 0, a.Length - 1, nth, comparer);
                    Sort.NthElement(d, dk, 0, b.Length - 1, nth, comparer);
                    var str = random.ToString(CSharpArrayFormatProvider.InvariantCulture);
                    Assert.IsTrue(a.IsEqual(c));
                    Assert.IsTrue(c.IsEqual(d));
                    Assert.IsTrue(d.IsEqual(dk));

                    Func <double, double, int> comparer2 = (x, y) => - x.CompareTo(y);
                    Sort.NthElement(e, 0, a.Length - 1, nth, comparer2, asc: false);
                    Sort.NthElement(f, fk, 0, b.Length - 1, nth, comparer2, asc: false);
                    Assert.IsTrue(a.IsEqual(e));
                    Assert.IsTrue(e.IsEqual(f));
                    Assert.IsTrue(f.IsEqual(fk));

                    Assert.IsTrue(a.IsEqual(c));
                    Assert.IsTrue(a.IsEqual(d));
                    Assert.IsTrue(a.IsEqual(e));
                    Assert.IsTrue(a.IsEqual(f));
                    Assert.IsTrue(a.IsEqual(fk));
                }
            }
        }