Example #1
0
    public TradeRoute[] SortTradeRoutes(TradeRoute[] givenRoute)
    {
        TradeRoute[] tempArray;
        if (givenRoute.Length > 1)
        {
            int[] pivot = { (int)Mathf.Floor(givenRoute.Length / 2), 0, 1 };
            tempArray = new TradeRoute[givenRoute.Length];
            for (int i = 0; i < givenRoute.Length; i++)
            {
                if (i != pivot[0])
                {
                    if (givenRoute[i].GetDistance() < givenRoute[pivot[0]].GetDistance())
                    {
                        //Debug.Log("iteration + " + pivot[1] + " :" + givenRoute[i].GetNames());
                        tempArray[pivot[1]] = givenRoute[i];
                        pivot[1]++;
                    }
                    else
                    {
                        //Debug.Log("if bigger iteration " + (tempArray.Length - pivot[2]) + ": " + givenRoute[i].GetNames());
                        tempArray[tempArray.Length - pivot[2]] = givenRoute[i];
                        pivot[2]++;
                    }
                }
            }

            //Create the two sub arrays
            tempArray[pivot[1]] = givenRoute[pivot[0]];
            TradeRoute[] small = new TradeRoute[pivot[1]];
            TradeRoute[] big   = new TradeRoute[pivot[2] - 1];
            //Debug.Log(givepivot[0]);
            for (int i = 0; i < small.Length; i++)
            {
                small[i] = tempArray[i];
            }
            small = SortTradeRoutes(small);
            small.CopyTo(tempArray, 0);
            for (int i = 0; i < big.Length; i++)
            {
                big[i] = tempArray[pivot[1] + 1 + i];
            }
            big = SortTradeRoutes(big);
            big.CopyTo(tempArray, pivot[1] + 1);
        }
        else
        {
            tempArray = givenRoute;
        }
        return(tempArray);
    }