Esempio n. 1
0
        public void CanSortArray(int[] input, int[] expected)
        {
            //Act
            MergeSortClass.MergeSort(input);

            //Assert
            Assert.Equal(expected, input);
        }
Esempio n. 2
0
    public override void cast()
    {
        GetNonStaticSegments();
        GetRays();
        m_lsRays = MergeSortClass.MergeSort(m_lsRays, Vector2.right);

        m_lsPoints = new List <Vector2>();
        GetIntersections();
    }
 public static bool Search(int num, int[] arr)
 {
     if (arr.Length == 0)
     {
         return(false);
     }
     arr = new MergeSortClass().Sort(arr);
     return(BinSearch(arr, num, 0, arr.Length - 1));
 }
Esempio n. 4
0
        public void MergeSort_Test()
        {
            var notSortedArray = GetNotSortedArray;

            var sortArray = GetNotSortedArray.OrderBy(x => x);

            var result = MergeSortClass <int> .Sort(notSortedArray.ToList());

            Assert.AreEqual(sortArray, result);
        }
Esempio n. 5
0
    public override void cast()
    {
        GetNonStaticSegments();
        GetRays();
        Vector2 vec = new Vector2(Mathf.Cos((orientation - opening) * Mathf.Deg2Rad), Mathf.Sin((orientation - opening) * Mathf.Deg2Rad));

        m_lsRays = MergeSortClass.MergeSort(m_lsRays, vec);

        m_lsPoints = new List <Vector2>();
        GetIntersections();
    }
Esempio n. 6
0
        static void Main(string[] args)
        {
            var array = new int[] { -1, 5, 7, 1, 100, 80, -20 };

            MergeSortClass.MergeSort(array, 0, array.Length - 1);
            for (int i = 0; i < array.Length; ++i)
            {
                Console.Write(array[i] + " ");
            }
            Console.ReadKey();
        }
Esempio n. 7
0
    private void Update()
    {
        GetNonStaticSegments();
        GetRays();
        Vector2 vec = new Vector2(Mathf.Cos((orientation - opening) * Mathf.Deg2Rad), Mathf.Sin((orientation - opening) * Mathf.Deg2Rad));

        m_lsRays = MergeSortClass.MergeSort(m_lsRays, vec);

        m_lsPoints = new List <Vector2>();
        GetIntersections();
        m_pcEdgeCollider2D.points = m_lsPoints.ToArray();
    }
Esempio n. 8
0
        public void CanSortAnEmptyArray()
        {
            //Arrange
            int[] testArray = new int[0];
            int[] expected  = new int[0];

            //Act
            MergeSortClass.MergeSort(testArray);

            //Assert
            Assert.Equal(expected, testArray);
        }
Esempio n. 9
0
        public void CanSortAnArrayWithNegativeIntegers()
        {
            //Arrange
            int[] testArray = new int[] { 5, -12, 0, -22, 17, 9, -7 };
            int[] expected  = new int[] { -22, -12, -7, 0, 5, 9, 17 };

            //Act
            MergeSortClass.MergeSort(testArray);

            //Assert
            Assert.Equal(expected, testArray);
        }
Esempio n. 10
0
        public void CanSortAnArrayOfLengthOne()
        {
            //Arrange
            int[] testArray = new int[] { 5 };
            int[] expected  = new int[] { 5 };

            //Act
            MergeSortClass.MergeSort(testArray);

            //Assert
            Assert.Equal(expected, testArray);
        }
Esempio n. 11
0
        public void Test_MergeSort()

        {
            int[] input  = { 1, 9, 5, 2, 11, 15 };
            int[] result = new MergeSortClass().Sort(input);
        }
Esempio n. 12
0
        public List <Point> calcShortestPath(int startX, int startY, int goalX, int goalY)
        {
            AStarCell startCell = map.getCell(startX, startY);
            AStarCell goalCell  = map.getCell(goalX, goalY);

            //Check if the start cell is also an obstacle (if it is, it is impossible to find a path)
            if (AStarCell.isObstacle(startCell))
            {
                UnityEngine.Debug.Log("start is null");
                return(null);
            }

            //Check if the goal cell is also an obstacle (if it is, it is impossible to find a path there)
            if (AStarCell.isObstacle(goalCell))
            {
                UnityEngine.Debug.Log("end is null");
                return(null);
            }

            startCell.reset();
            startCell.setDistanceFromStart(0);
            closedList.Clear();
            openList.Clear();
            openList.Add(startCell);

            //while we haven't reached the goal yet
            while (openList.Count() != 0)
            {
                //get the first Cell from non-searched Cell list, sorted by lowest distance from our goal as guessed by our heuristic
                AStarCell current = openList[0];

                // check if our current Cell location is the goal Cell. If it is, we are done.
                if (current.getX() == goalX && current.getY() == goalY)
                {
                    return(reconstructPath(current));
                }

                //move current Cell to the closed (already searched) list
                openList.Remove(current);
                closedList.Add(current);

                //go through all the current Cells neighbors and calculate if one should be our next step
                foreach (AStarCell neighbor in map.getNeighborList(current))
                {
                    bool neighborIsBetter;

                    //if we have already searched this Cell, don't bother and continue to the next one
                    if (closedList.Contains(neighbor))
                    {
                        continue;
                    }

                    // calculate how long the path is if we choose this neighbor as the next step in the path
                    float neighborDistanceFromStart = (current.getDistanceFromStart() + AStarMap.getDistanceBetween(current, neighbor));

                    //add neighbor to the open list if it is not there
                    if (!openList.Contains(neighbor))
                    {
                        neighbor.reset();
                        openList.Add(neighbor);
                        neighborIsBetter = true;
                        //if neighbor is closer to start it could also be better
                    }
                    else if (neighborDistanceFromStart < current.getDistanceFromStart())
                    {
                        neighborIsBetter = true;
                    }
                    else
                    {
                        neighborIsBetter = false;
                    }
                    // set neighbors parameters if it is better
                    if (neighborIsBetter)
                    {
                        neighbor.setPreviousCell(current);
                        neighbor.setDistanceFromStart(neighborDistanceFromStart);
                        neighbor.setHeuristicDistanceFromGoal(heuristic.getEstimatedDistanceToGoal(
                                                                  neighbor.getX(), neighbor.getY(), goalCell.getX(), goalCell.getY()));

                        // csharp List.Sort use QuickSort, which is unstable,
                        // but in java implement ArrayList.sort use MergeSort, which is stable,
                        // so here use MergeSort to generate the same result as in java implement
                        MergeSortClass.MergeSort(openList);
                    }
                }
            }

            UnityEngine.Debug.Log("path is null");
            return(null);
        }