void Quicksort(Tree[] array, int left, int right)
 {
     if (left < right)
     {
         int p = Partition(array, left, right);
         Quicksort(array, left, p - 1);
         Quicksort(array, p + 1, right);
     }
 }
        public void Heapsort(Tree[] v)
        {
            buildMaxHeap(v);
            int n = v.Length;

            for (int i = v.Length - 1; i > 0; i--)
            {
                swap(v, i, 0);
                maxHeapify(v, 0, --n);
            }
        }
        public void DrawPath(Node goal,ref Grid grid,ref Tree[] _trees)
        {
            Point startingPoint = StateToPoint(nodeState.ORIGIN);

            int pointX = startingPoint.X;
            int pointY = startingPoint.Y;

            path.Add(origin);

            if (pointX == -1 && pointY == -1)
            {
                return;
            }

            while (true)
            {

                Point lowestPoint = Point.Zero;
                int lowest = 10000;

                foreach (Point movePoint in CheckStep(pointX, pointY))
                {
                    int count = _squares[movePoint.X, movePoint.Y].distanceSteps;
                    if (count < lowest)
                    {
                        lowest = count;
                        lowestPoint.X = movePoint.X;
                        lowestPoint.Y = movePoint.Y;
                    }
                }
                if (lowest != 10000)
                {
                    _squares[lowestPoint.X, lowestPoint.Y].hasPath = true;
                    _squares[lowestPoint.X, lowestPoint.Y].ChangeTexture(ImageLibrary.getInstance().getImage("Clear"));
                    path.Add(_squares[lowestPoint.X, lowestPoint.Y]);
                    pointX = lowestPoint.X;
                    pointY = lowestPoint.Y;
                }
                else
                {
                    Search(origin, goal, ref grid, this.game, ref _trees, ref this.path);
                    return;
                }

                if (_squares[pointX, pointY].state == nodeState.GOAL)
                {
                    _squares[pointX, pointY].ChangeTexture(ImageLibrary.getInstance().getImage("Origin"));
                    origin.ChangeTexture(ImageLibrary.getInstance().getImage("Origin"));
                    path.Add(_squares[pointX, pointY]);
                    CreateTrees(ref _trees);
                    break;
                }
            }
        }
        private void maxHeapify(Tree[] v, int pos, int n)
        {
            int max = 2 * pos + 1, right = max + 1;
            if (max < n)
            {
                if (right < n && v[max].module > v[right].module)
                    max = right;

                if (v[max].module < v[pos].module)
                {
                    swap(v, max, pos);
                    maxHeapify(v, max, n);
                }
            }
        }
        int Partition(Tree[] array, int left, int right)
        {
            Tree s = array[right];
            double p = s.module;
            int i = left;

            for (int j = left; j < right; j++)
            {
                if (array[j].module > p)
                {
                    Tree temp = array[i];
                    array[i] = array[j];
                    array[j] = temp;
                    i++;
                }
            }

            array[right] = array[i];
            array[i] = s;
            return i;
        }
 private void buildMaxHeap(Tree[] v)
 {
     for (int i = v.Length / 2 - 1; i >= 0; i--)
         maxHeapify(v, i, v.Length);
 }
 public void Sort(Tree[] array)
 {
     Heapsort(array);
 }
 public static void swap(Tree[] v, int j, int aposJ)
 {
     Tree aux = v[j];
     v[j] = v[aposJ];
     v[aposJ] = aux;
 }
 public void Sort(Tree[] array)
 {
     Quicksort(array,0, array.Length-1);
 }
        private void CreateTrees(ref Tree[] _trees)
        {
            int length = trees.Length;
            _trees = new Tree[length];

            for (int i = 0; i < length; i++)
            {
                _trees[i] = new Tree(game, new Vector3(1, 1, 1), new Vector3(0, 0, 0), new Vector3(trees[i].getPosition().X, 0, trees[i].getPosition().Z), game.cam);
            }
        }
        public void Search(Node origin, Node goal, ref Grid grid, Game1 game, ref Tree[] allTrees, ref List<Node> _path)
        {
            this.path = _path;
            this.game = game;

            allNodes.Clear();

            ResetNodes();

            this.origin = origin;
            this._squares = grid.nodes;
            this.row = grid.Rows;
            this.column = grid.Columns;

            foreach (Point p in GetPoints())
            {
                allNodes.Add(_squares[p.X,p.Y]);
                _squares[p.X, p.Y].ChangeTexture(ImageLibrary.getInstance().getImage("Floor"));
            }

            allNodes.Remove(goal);
            allNodes.Remove(origin);

            foreach (Node tree in SortTrees(6))
            {
                tree.state = nodeState.CLOSED;
            }

            goal.state = nodeState.GOAL;
            origin.state = nodeState.ORIGIN;

            Pathfind();
            DrawPath(goal,ref grid,ref allTrees);
        }