private static int PartitionASC(IComparable[] a, int lo, int hi)
        {
            int         i = lo, j = hi + 1;
            IComparable v = a[lo];

            while (true)
            {
                while (SortHelper.Less(a[++i], v))
                {
                    if (i == hi)
                    {
                        break;
                    }
                }
                while (SortHelper.Less(v, a[--j]))
                {
                    if (j == lo)
                    {
                        break;
                    }
                }
                if (i >= j)
                {
                    break;
                }
                SortHelper.Exchange(a, i, j);
            }
            SortHelper.Exchange(a, lo, j);
            return(j);
        }
Esempio n. 2
0
        private static void SortASC(IComparable[] a, int lo, int hi)
        {
            if (hi <= lo)
            {
                return;
            }
            int         lt = lo, gt = hi;
            int         i = lo;
            IComparable v = a[lo];

            while (i <= gt)
            {
                int cmp = a[i].CompareTo(v);
                if (cmp < 0)
                {
                    SortHelper.Exchange(a, lt++, i++);
                }
                else if (cmp > 0)
                {
                    SortHelper.Exchange(a, i, gt--);
                }
                else
                {
                    i++;
                }
            }
            SortASC(a, lo, lt - 1);
            SortASC(a, gt + 1, hi);
        }
        private static int PartitionDESC <Key>(Key[] a, IComparer <Key> c, int lo, int hi) where Key : class
        {
            int i = lo, j = hi + 1;
            Key v = a[lo];

            while (true)
            {
                while (SortHelper.Greater(c, a[++i], v))
                {
                    if (i == hi)
                    {
                        break;
                    }
                }
                while (SortHelper.Greater(c, v, a[--j]))
                {
                    if (j == lo)
                    {
                        break;
                    }
                }
                if (i >= j)
                {
                    break;
                }
                SortHelper.Exchange(a, i, j);
            }
            SortHelper.Exchange(a, lo, j);
            return(j);
        }
Esempio n. 4
0
        private static void SortDESC <Key>(Key[] a, IComparer <Key> c, int lo, int hi) where Key : class
        {
            if (hi <= lo)
            {
                return;
            }
            int lt = lo, gt = hi;
            int i = lo;
            Key v = a[lo];

            while (i <= gt)
            {
                int cmp = c.Compare(a[i], v);
                if (cmp > 0)
                {
                    SortHelper.Exchange(a, lt++, i++);
                }
                else if (cmp < 0)
                {
                    SortHelper.Exchange(a, i, gt--);
                }
                else
                {
                    i++;
                }
            }
            SortDESC(a, c, lo, lt - 1);
            SortDESC(a, c, gt + 1, hi);
        }
        /// <summary>
        /// Rearranges an array of objects in uniformly random order
        /// (under the assumption that <tt>Math.random()</tt> generates independent
        /// and uniformly distributed numbers between 0 and 1).
        /// </summary>
        /// <param name="a">the array to be shuffled</param>
        public static void Do <Key>(Key[] a)
        {
            int N = a.Length;

            for (int i = 0; i < N; i++)
            {
                int r = random.Next(i + 1);
                SortHelper.Exchange(a, r, i);
            }
        }
        /// <summary>
        /// Rearranges the array in ascending order, using a comparer.
        /// </summary>
        /// <param name="a">The array to be sorted</param>
        /// <param name="c">The comparer that specifying the order</param>
        private static void SortASC <Key>(Key[] a, IComparer <Key> c) where Key : class
        {
            int N = a.Length;

            for (int i = 0; i < N; i++)
            {
                for (int j = i; j > 0 && SortHelper.Less(c, a[j], a[j - 1]); j--)
                {
                    SortHelper.Exchange(a, j, j - 1);
                }
            }
        }
        /// <summary>
        /// Rearranges the array in ascending order, using the natural order.
        /// </summary>
        /// <param name="a">The array to be sorted</param>
        private static void SortASC(IComparable[] a)
        {
            int N = a.Length;

            for (int i = 0; i < N; i++)
            {
                for (int j = i; j > 0 && SortHelper.Less(a[j], a[j - 1]); j--)
                {
                    SortHelper.Exchange(a, j, j - 1);
                }
            }
        }
Esempio n. 8
0
        /// <summary>
        /// Rearranges the array in ascending order, using the natural order.
        /// </summary>
        /// <param name="a">The array to be sorted</param>
        private static void SortASC(IComparable[] a)
        {
            int N = a.Length;

            for (int i = 0; i < N; i++)
            {
                int min = i;
                for (int j = i + 1; j < N; j++)
                {
                    if (SortHelper.Less(a[j], a[min]))
                    {
                        min = j;
                    }
                }
                SortHelper.Exchange(a, min, i);
            }
        }
Esempio n. 9
0
        /// <summary>
        /// Rearranges the array in descending order, using a comparer.
        /// </summary>
        /// <param name="a">The array to be sorted</param>
        /// <param name="c">the comparer that specifying the order</param>
        private static void SortDESC <Key>(Key[] a, IComparer <Key> c) where Key : class
        {
            int N = a.Length;

            for (int i = 0; i < N; i++)
            {
                int max = i;
                for (int j = i + 1; j < N; j++)
                {
                    if (SortHelper.Greater(c, a[j], a[max]))
                    {
                        max = j;
                    }
                }
                SortHelper.Exchange(a, i, max);
            }
        }
Esempio n. 10
0
        /// <summary>
        /// Rearranges the array in descending order, using the natural order.
        /// </summary>
        /// <param name="a">The array to be sorted</param>
        private static void SortDESC(IComparable[] a)
        {
            int N = a.Length;

            for (int i = 0; i < N; i++)
            {
                int max = i;
                for (int j = i + 1; j < N; j++)
                {
                    if (SortHelper.Greater(a[j], a[max]))
                    {
                        max = j;
                    }
                }
                SortHelper.Exchange(a, max, i);
            }
        }
Esempio n. 11
0
        /// <summary>
        /// Rearranges the array in ascending order, using a comparer.
        /// </summary>
        /// <param name="a">The array to be sorted</param>
        /// <param name="c">the comparer that specifying the order</param>
        private static void SortASC <Key>(Key[] a, IComparer <Key> c) where Key : class
        {
            int N = a.Length;

            for (int i = 0; i < N; i++)
            {
                int min = i;
                for (int j = i + 1; j < N; j++)
                {
                    if (SortHelper.Less(c, a[j], a[min]))
                    {
                        min = j;
                    }
                }
                SortHelper.Exchange(a, i, min);
            }
        }
Esempio n. 12
0
        /// <summary>
        /// Rearranges the array in ascending order, using the natural order.
        /// </summary>
        /// <param name="a">The array to be sorted</param>
        private static void SortASC(IComparable[] a)
        {
            int N = a.Length;

            int h = 1;

            while (h < N / 3)
            {
                h = h * 3 + 1;
            }

            while (h >= 1)
            {
                for (int i = h; i < N; i++)
                {
                    for (int j = i; j >= h && SortHelper.Less(a[j], a[j - h]); j -= h)
                    {
                        SortHelper.Exchange(a, j, j - h);
                    }
                }
                h /= 3;
            }
        }
Esempio n. 13
0
        /// <summary>
        /// Rearranges the array in descending order, using a comparer.
        /// </summary>
        /// <param name="a">The array to be sorted</param>
        /// <param name="c">The comparer that specifying the order</param>
        private static void SortDESC <Key>(Key[] a, IComparer <Key> c) where Key : class
        {
            int N = a.Length;

            int h = 1;

            while (h < N / 3)
            {
                h = h * 3 + 1;
            }

            while (h >= 1)
            {
                for (int i = h; i < N; i++)
                {
                    for (int j = i; j >= h && SortHelper.Greater(c, a[j], a[j - h]); j -= h)
                    {
                        SortHelper.Exchange(a, j, j - h);
                    }
                }
                h /= 3;
            }
        }
Esempio n. 14
0
 private static void Exchange(Object[] a, int i, int j)
 {
     SortHelper.Exchange(a, i - 1, j - 1);
 }