Esempio n. 1
0
            /// <summary> Comb sort is an improved variant of Bubble Sort. </summary>
            /// <param name="_order"> Ascending or descending sorting order. </param>
            /// <param name="_array"> Can insert single values to fill a new array or pass an array that already exists. </param>
            public T[] CombSort <T>(SortingOrder _order, params T[] _array) where T : IComparable
            {
                float beforeTime = Time.realtimeSinceStartup;

                double gap   = _array.Length;
                bool   swaps = true;

                while (gap > 1 || swaps)
                {
                    gap /= 1.247330950103979;

                    if (gap < 1)
                    {
                        gap = 1;
                    }

                    swaps = false;
                    int i = 0;

                    while (i + gap < _array.Length)
                    {
                        int igap = i + (int)gap;

                        if (_array[i].CompareTo(_array[igap]).Equals(_order.Equals(SortingOrder.Ascending) ? 1 : -1))
                        {
                            T temp = _array[i];
                            _array[i]    = _array[igap];
                            _array[igap] = temp;
                            swaps        = true;
                        }

                        ++i;
                    }
                }

                if (UtilitiesGenInstance.debugMode)
                {
                    Debug.Log("Sorting time: " + string.Format("{0:0.000}", (Time.realtimeSinceStartup - beforeTime) * 1000) + " ms\n");
                }

                return(_array);
            }
Esempio n. 2
0
        public T[] Sort <T>(SortingOrder _order, params T[] _array) where T : IComparable
        {
            float beforeTime = Time.realtimeSinceStartup;

            double gap   = _array.Length;
            bool   swaps = true;

            sbyte order = (sbyte)(_order.Equals(SortingOrder.Ascending) ? 1 : -1);

            while (gap > 1 || swaps)
            {
                gap /= 1.247330950103979;

                if (gap < 1)
                {
                    gap = 1;
                }

                swaps = false;
                int i = 0;

                while (i + gap < _array.Length)
                {
                    int igap = i + (int)gap;

                    if (_array[i].CompareTo(_array[igap]).Equals(order))
                    {
                        T temp = _array[i];
                        _array[i]    = _array[igap];
                        _array[igap] = temp;
                        swaps        = true;
                    }

                    ++i;
                }
            }

            Debug.Log(CustomLibrary.Utils.BuildString("Sorting time: ", string.Format("{0:0.000}", (Time.realtimeSinceStartup - beforeTime) * 1000), " ms\n"));

            return(_array);
        }