Exemple #1
0
        private static void quickSortImpl <T>(T[] values, int low, int high, CompareFunc <T> comparator)
        {
            int i;
            int j;
            T   v;

            //
            if (high - low > 4)
            {
                i = (high + low) / 2;
                if (comparator.Invoke(values[low], values[i]) != 0)
                {
                    swap(values, low, i);
                }
                if (comparator.Invoke(values[low], values[high]) != 0)
                {
                    swap(values, low, high);
                }
                if (comparator.Invoke(values[i], values[high]) != 0)
                {
                    swap(values, i, high);
                }
                //
                j = high - 1;
                swap(values, i, j);
                i = low;
                v = values[j];
                while (true)
                {
                    while (comparator.Invoke(v, values[++i]) != 0)
                    {
                    }
                    while (comparator.Invoke(values[--j], v) != 0)
                    {
                    }
                    if (j < i)
                    {
                        break;
                    }
                    swap(values, i, j);
                }
                swap(values, i, high - 1);
                quickSortImpl(values, low, j, comparator);
                quickSortImpl(values, i + 1, high, comparator);
            }
        }
Exemple #2
0
        private static void insertionSort <T>(T[] values, int low, int high, CompareFunc <T> comparator)
        {
            T v;

            for (int i = low + 1, j; i <= high; i++)
            {
                v = values[i];
                j = i;
                //
                while ((j > low) && comparator.Invoke(values[j - 1], v) > 0)
                {
                    values[j] = values[j - 1];
                    j--;
                }
                values[j] = v;
            }
        }
Exemple #3
0
        /// <summary>
        /// 比较
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public int Compare(T a, T b)
        {
            int?ret = CompareFunc?.Invoke(a.Data, b.Data);

            return((int)ret);
        }