Exemple #1
0
        /// <summary>
        /// 快速排序(非递归版)(不稳定排序)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        public static void Sort_V <T>(T[] data) where T : IComparable
        {
            Stack <int> stack = new Stack <int>(data.Length / 2);
            T           pivot;
            int         low = 0;
            int         high = data.Length - 1;
            int         start, end;

            stack.Push(high);
            stack.Push(low);

            while (stack.Count > 0)
            {
                start = low = stack.Pop();
                end   = high = stack.Pop();

                if (low >= high)
                {
                    continue;
                }
                if (high - low + 1 <= 20)
                {
                    InsertSort.Sort(data, low, high);
                    continue;
                }

                pivot = data[low];
                while (low < high)
                {
                    while (low < high && data[high].CompareTo(pivot) >= 0)
                    {
                        high--;
                    }
                    data[low] = data[high];

                    while (low < high && data[low].CompareTo(pivot) <= 0)
                    {
                        low++;
                    }
                    data[high] = data[low];
                }
                data[low] = pivot;
                stack.Push(low - 1);
                stack.Push(start);
                stack.Push(end);
                stack.Push(low + 1);
            }
        }
Exemple #2
0
        private static void Sort <T>(T[] data, int low, int high) where T : IComparable
        {
            if (low >= high)
            {
                return;
            }
            if (high - low + 1 <= 20)
            {
                InsertSort.Sort(data, low, high);
                return;
            }

            int split = Partition(data, low, high);

            Sort(data, low, split - 1);
            Sort(data, split + 1, high);
        }