示例#1
0
        public static void MainTest(string[] args)
        {
            TextInput StdIn = new TextInput();

            string[] a = StdIn.ReadAllStrings();
            Insertion.Sort(a);
            OrderHelper.Show(a);
        }
示例#2
0
文件: MergeX.cs 项目: zzhi/Algs4Net
        private static void Sort(IComparable[] src, IComparable[] dst, int lo, int hi)
        {
            // if (hi <= lo) return;
            if (hi <= lo + CUTOFF)
            {
                Insertion.Sort(dst, lo, hi);
                return;
            }
            int mid = lo + (hi - lo) / 2;

            Sort(dst, src, lo, mid);
            Sort(dst, src, mid + 1, hi);

            if (!OrderHelper.Less(src[mid + 1], src[mid]))
            {
                Array.Copy(src, lo, dst, lo, hi - lo + 1);
                return;
            }
            Merge(src, dst, lo, mid, hi);
        }
示例#3
0
文件: MergeX.cs 项目: zzhi/Algs4Net
        private static void Sort <T>(T[] src, T[] dst, int lo, int hi, Comparer <T> comparator)
        {
            // if (hi <= lo) return;
            if (hi <= lo + CUTOFF)
            {
                Insertion.Sort(dst, lo, hi, comparator);
                return;
            }
            int mid = lo + (hi - lo) / 2;

            Sort(dst, src, lo, mid, comparator);
            Sort(dst, src, mid + 1, hi, comparator);

            if (!OrderHelper.Less(src[mid + 1], src[mid], comparator))
            {
                Array.Copy(src, lo, dst, lo, hi - lo + 1);
                return;
            }

            Merge(src, dst, lo, mid, hi, comparator);
        }
示例#4
0
文件: QuickX.cs 项目: zzhi/Algs4Net
        private static void Sort(IComparable[] a, int lo, int hi)
        {
            int N = hi - lo + 1;

            // cutoff to insertion sort
            if (N <= CUTOFF)
            {
                Insertion.Sort(a, lo, hi);
                return;
            }

            // use median-of-3 as partitioning element
            else if (N <= 40)
            {
                int m = median3(a, lo, lo + N / 2, hi);
                OrderHelper.Exch(a, m, lo);
            }

            // use Tukey ninther as partitioning element
            else
            {
                int eps     = N / 8;
                int mid     = lo + N / 2;
                int m1      = median3(a, lo, lo + eps, lo + eps + eps);
                int m2      = median3(a, mid - eps, mid, mid + eps);
                int m3      = median3(a, hi - eps - eps, hi - eps, hi);
                int ninther = median3(a, m1, m2, m3);
                OrderHelper.Exch(a, ninther, lo);
            }

            // Bentley-McIlroy 3-way partitioning
            int         i = lo, j = hi + 1;
            int         p = lo, q = hi + 1;
            IComparable v = a[lo];

            while (true)
            {
                while (OrderHelper.Less(a[++i], v))
                {
                    if (i == hi)
                    {
                        break;
                    }
                }
                while (OrderHelper.Less(v, a[--j]))
                {
                    if (j == lo)
                    {
                        break;
                    }
                }

                // pointers cross
                if (i == j && OrderHelper.Eq(a[i], v))
                {
                    OrderHelper.Exch(a, ++p, i);
                }
                if (i >= j)
                {
                    break;
                }

                OrderHelper.Exch(a, i, j);
                if (OrderHelper.Eq(a[i], v))
                {
                    OrderHelper.Exch(a, ++p, i);
                }
                if (OrderHelper.Eq(a[j], v))
                {
                    OrderHelper.Exch(a, --q, j);
                }
            }


            i = j + 1;
            for (int k = lo; k <= p; k++)
            {
                OrderHelper.Exch(a, k, j--);
            }
            for (int k = hi; k >= q; k--)
            {
                OrderHelper.Exch(a, k, i++);
            }

            Sort(a, lo, j);
            Sort(a, i, hi);
        }