示例#1
0
        public static void Sort <T>(T[] a, int lo, int hi, Comparison <T> compare)
        {
            if (lo >= hi)
            {
                return;
            }

            if (hi - lo < 7)
            {
                InsertionSort.Sort(a, lo, hi, compare);
                return;
            }


            int i = lo, lt = lo, gt = hi;
            var v = a[lo];

            while (i <= gt)
            {
                if (SortUtil.IsLessThan(a[i], v, compare))
                {
                    SortUtil.Exchange(a, i++, lt++);
                }
                else if (SortUtil.IsLessThan(v, a[i], compare))
                {
                    SortUtil.Exchange(a, i, gt--);
                }
                else
                {
                    i++;
                }
            }

            Sort(a, lo, lt - 1, compare);
            Sort(a, gt + 1, hi, compare);
        }
示例#2
0
        public static void Sort(int[] data, int maxValue)
        {
            List <int>[] buckets = new List <int> [10];

            for (int i = 0; i < buckets.Length; i++)
            {
                buckets[i] = new List <int>();
            }

            foreach (int n in data)
            {
                buckets[10 * n / maxValue].Add(n);
            }

            int count = 0;

            for (int i = 0; i < buckets.Length; i++)
            {
                int[] array = buckets[i].ToArray();
                InsertionSort.Sort(array);
                Array.Copy(array, 0, data, count, array.Length);
                count += array.Length;
            }
        }