Ejemplo n.º 1
0
        public void BubbleSortTest()
        {
            var sortedList = SortAlgorithms.BubbleSort(new List <int>()
            {
                100, 2, 4473, 1, 474, 5, 7, 3, 55, 4, 8, 12
            });
            var list = sortedList as int[] ?? sortedList.ToArray();

            Assert.AreEqual(list.ElementAt(1), 2);
            Assert.AreEqual(list.ElementAt(6), 8);
            Assert.AreEqual(list.Count(), 12);
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            Console.WriteLine("---Sorting Algorithms---");
            var iArr = new int[] { 3, 2, 1, 9 };

            //SortAlgorithms.InsertionSort(iArr);
            iArr = new int[] { 3, 2, 1, 9, 47, 37 };
            //SortAlgorithms.QuickSort(iArr);

            //SortAlgorithms.HeapSort(iArr);

            var res = SortAlgorithms.MergeSort(iArr);
        }
Ejemplo n.º 3
0
 private void sortCB_SelectedIndexChanged(object sender, EventArgs e)
 {
     if (sortCB.SelectedIndex == 0)
     {
         sortAlgorithm = SortAlgorithms.SORT_ICOMPARABLE;
     }
     else if (sortCB.SelectedIndex == 1)
     {
         sortAlgorithm = SortAlgorithms.SORT_LINQ;
     }
     else if (sortCB.SelectedIndex == 2)
     {
         sortAlgorithm = SortAlgorithms.SORT_LAMBDA;
     }
 }
Ejemplo n.º 4
0
        public void lsListWasNotModifiedTest()
        {
            int        value  = 777;
            List <int> source = new List <int>()
            {
                1, 2, 4, 5, 7, 77, 776, 778, 10
            };

            List <int> copy       = source.ToList();
            int        valueIndex = SearchAlgorithms.LinearSearch <int>(source, value);

            bool isEqualsByElement = SortAlgorithms.EqualsByElements(source, copy);

            Assert.IsTrue(isEqualsByElement);
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            var test = new int[5] {
                5, 2, 1, 4, 3
            };

            SortAlgorithms <int> .MergeSort(test);

            foreach (var item in test)
            {
                Console.Write($"{item} ");
            }

            Console.ReadKey();
        }
Ejemplo n.º 6
0
        public void TestLowBubble()
        {
            SetUp1(false);
            SortAlgorithms.BubbleSort(arrayTest);

            bool same = true;

            for (int i = 0; i < arrayTest.Length && same; i++)
            {
                if (arrayTest[i] != solutionLow[i])
                {
                    same = false;
                }
            }

            Assert.IsTrue(same);
        }
Ejemplo n.º 7
0
        public void TestLowRepBubble()
        {
            SetUp3(false);
            SortAlgorithms.BubbleSort(arrayTest);

            bool same = true;

            for (int i = 0; i < arrayTest.Length && same && (i + 1 < arrayTest.Length); i++)
            {
                if (arrayTest[i] > arrayTest[i + 1])
                {
                    same = false;
                }
            }

            Assert.IsTrue(same);
        }
Ejemplo n.º 8
0
        public void TestHighRepInsertion()
        {
            SetUp3(true);
            SortAlgorithms.InsertionSort(arrayTest);

            bool same = true;

            for (int i = 0; i < arrayTest.Length && same && (i + 1 < arrayTest.Length); i++)
            {
                if (arrayTest[i] > arrayTest[i + 1])
                {
                    same = false;
                }
            }

            Assert.IsTrue(same);
        }
Ejemplo n.º 9
0
        public void TestHighInsertion()
        {
            SetUp1(true);
            SortAlgorithms.InsertionSort(arrayTest);

            bool same = true;

            for (int i = 0; i < arrayTest.Length && same; i++)
            {
                if (arrayTest[i] != solutionHigh[i])
                {
                    same = false;
                }
            }

            Assert.IsTrue(same);
        }
Ejemplo n.º 10
0
    public static void GrahamScanSortingTest(List <Vector3> points, List <Vector3> ccwPolygonPoints)
    {
        if (points.Count < 3)
        {
            return;
        }
        if (points.Count == 3)
        {
            for (int i = 0; i < points.Count; i++)
            {
                ccwPolygonPoints.Add(points[i]);
            }
            return;
        }
        //find the point having the lowest y coordinate (use x for euqual cases if any)
        //put it at the end of the list and remove it (this will be point P)
        Vector3 tmp = Vector3.zero;

        for (int i = points.Count - 1; i >= 0; i--)
        {
            if (points[points.Count - 1].z > points[i].z)
            {
                //swap
                tmp = points[points.Count - 1];
                points[points.Count - 1] = points[i];
                points[i] = tmp;
            }
            else if (points[points.Count - 1].z == points[i].z)
            {
                if (points[points.Count - 1].x < points[i].x)
                {
                    tmp = points[points.Count - 1];
                    points[points.Count - 1] = points[i];
                    points[i] = tmp;
                }
            }
        }
        //sort the remaining point according the angle they form with the point P
        Vector3 P = points[points.Count - 1];

        points.RemoveAt(points.Count - 1);
        SortAlgorithms.HeapSortGrahamScan(points, ref P);
        points.Add(P);        //just a test
    }
Ejemplo n.º 11
0
    /// <summary>
    /// Performs the computation of the convex hull of the passed points
    /// through Graham scan http://en.wikipedia.org/wiki/Graham_scan
    /// the returned points are the vertices of the polygon sorted
    /// counter clockwise
    /// </summary>
    /// <param name="points"> The points of which to find the convex hull that contains them
    /// A <see cref="List<Vector3>"/>
    /// </param>
    /// <returns>
    /// A <see cref="List<Vector3>"/> The list of counter clockwise sorted vertices of the convex hull polygon
    /// </returns>
    public static List <Vector3> GrahamScan(List <Vector3> points)
    {
        List <Vector3> ccwPolygonPoints = new List <Vector3> ();

        /*
         * if(points.Count < 3)
         * {
         *      return ;
         * }
         * if(points.Count == 3)
         * {
         *      for(int i=0; i < points.Count; i++)
         *      {
         *              ccwPolygonPoints.Add(points[i]);
         *      }
         *      return ;
         * }
         */
        if (points.Count > 3)
        {
            //find the point having the lowest y coordinate (use x for euqual cases if any)
            //put it at the end of the list and remove it (this will be point P)
            Vector3 tmp = Vector3.zero;
            for (int i = points.Count - 1; i >= 0; i--)
            {
                if (points[points.Count - 1].z > points[i].z)
                {
                    //swap
                    tmp = points[points.Count - 1];
                    points[points.Count - 1] = points[i];
                    points[i] = tmp;
                }
                else if (points[points.Count - 1].z == points[i].z)
                {
                    if (points[points.Count - 1].x < points[i].x)
                    {
                        tmp = points[points.Count - 1];
                        points[points.Count - 1] = points[i];
                        points[i] = tmp;
                    }
                }
            }
            //sort the remaining point according the angle they form with the point P
            Vector3 P = points[points.Count - 1];
            points.RemoveAt(points.Count - 1);
            SortAlgorithms.HeapSortGrahamScan(points, ref P);
            ccwPolygonPoints.Add(P);
            ccwPolygonPoints.Add(points[0]);
            //points.Add(P);
            int k = 1;
            while (k < points.Count)
            {
                //Debug.Log("while graham scan");
                if (Vector3.Cross(ccwPolygonPoints[ccwPolygonPoints.Count - 1] - ccwPolygonPoints[ccwPolygonPoints.Count - 2],
                                  points[k] - ccwPolygonPoints[ccwPolygonPoints.Count - 2]).y < -SCAN_EPSILON)            //Left turn, it should be > 0  but I am using 3D coordinate using z as y axis reported in the algorithm
                {
                    ccwPolygonPoints.Add(points[k]);
                    k++;
                }
                else
                {
                    ccwPolygonPoints.RemoveAt(ccwPolygonPoints.Count - 1);
                    if (ccwPolygonPoints.Count == 1)                   //in order to make the algorithm more robust
                    {
                        ccwPolygonPoints.Add(points[k]);
                        k++;
                    }
                }
            }
        }
        else if (points.Count == 3)
        {
            for (int i = 0; i < points.Count; i++)
            {
                ccwPolygonPoints.Add(points[i]);
            }
        }
        return(ccwPolygonPoints);
    }
Ejemplo n.º 12
0
 public void TestQuickSort()
 {
     int[] originalList = new int[] { 5, 3, 4, 1, 2 };
     SortAlgorithms.QuickSort(originalList, 0, originalList.Length - 1);
     CollectionAssert.AreEqual(sortedList.ToArray(), originalList);
 }
Ejemplo n.º 13
0
 public void TestMergeSortEmptyList()
 {
     originalList = new List <int>();
     originalList = SortAlgorithms.InsertionSort(originalList);
     CollectionAssert.AreEqual(new List <int>(), originalList);
 }
Ejemplo n.º 14
0
 public void TestMergeSort()
 {
     originalList = new List <int>(new int[] { 5, 4, 3, 1, 2 });
     originalList = SortAlgorithms.MergeSort(originalList);
     CollectionAssert.AreEqual(sortedList, originalList);
 }
Ejemplo n.º 15
0
 public void TestInsertionSortInPlace()
 {
     originalList = new List <int>(new int[] { 5, 4, 3, 1, 2 });
     SortAlgorithms.InsertionSortInPlace(originalList);
     CollectionAssert.AreEqual(sortedList, originalList);
 }
Ejemplo n.º 16
0
 public void Init()
 {
     sortAlgos = new SortAlgorithms();
 }
        public void WhenIRunMergesortWithTheTaskParallelLibrary()
        {
            //Assert.Fail("Not implemented");

            SortAlgorithms.MergesortTpl(_items);
        }
 public void WhenIRunMergesort()
 {
     SortAlgorithms.Mergesort(_items);
 }