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. 2
0
        private static void MergeDESC <Key>(Key[] a, Key[] aux, IComparer <Key> c, int lo, int mid, int hi) where Key : class
        {
            if (!SortHelper.Greater(c, a[mid + 1], a[mid]))
            {
                return;
            }

            for (int k = lo; k <= hi; k++)
            {
                aux[k] = a[k];
            }

            int i = lo, j = mid + 1;

            for (int k = lo; k <= hi; k++)
            {
                if (i > mid)
                {
                    a[k] = aux[j++];
                }
                else if (j > hi)
                {
                    a[k] = aux[i++];
                }
                else if (SortHelper.Greater(c, aux[j], aux[i]))
                {
                    a[k] = aux[j++];
                }
                else
                {
                    a[k] = aux[i++];
                }
            }
        }
        private static int PartitionDESC(IComparable[] a, int lo, int hi)
        {
            int         i = lo, j = hi + 1;
            IComparable v = a[lo];

            while (true)
            {
                while (SortHelper.Greater(a[++i], v))
                {
                    if (i == hi)
                    {
                        break;
                    }
                }
                while (SortHelper.Greater(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 MergeDESC(IComparable[] a, IComparable[] aux, int lo, int mid, int hi)
        {
            if (!SortHelper.Greater(a[mid + 1], a[mid]))
            {
                return;
            }

            for (int k = lo; k <= hi; k++)
            {
                aux[k] = a[k];
            }

            int i = lo, j = mid + 1;

            for (int k = lo; k <= hi; k++)
            {
                if (i > mid)
                {
                    a[k] = aux[j++];
                }
                else if (j > hi)
                {
                    a[k] = aux[i++];
                }
                else if (SortHelper.Greater(aux[j], aux[i]))
                {
                    a[k] = aux[j++];
                }
                else
                {
                    a[k] = aux[i++];
                }
            }
        }
        /// <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++)
            {
                for (int j = i; j > 0 && SortHelper.Greater(c, a[j], a[j - 1]); j--)
                {
                    SortHelper.Exchange(a, j, j - 1);
                }
            }
        }
        /// <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++)
            {
                for (int j = i; j > 0 && SortHelper.Greater(a[j], a[j - 1]); j--)
                {
                    SortHelper.Exchange(a, j, j - 1);
                }
            }
        }
Esempio n. 7
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. 8
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. 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;

            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. 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;

            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(a[j], a[j - h]); j -= h)
                    {
                        SortHelper.Exchange(a, j, j - h);
                    }
                }
                h /= 3;
            }
        }
Esempio n. 11
0
 private static bool Greater <Key>(Key[] pq, IComparer <Key> c, int i, int j) where Key : class
 {
     return(SortHelper.Greater <Key>(c, pq[i - 1], pq[j - 1]));
 }
Esempio n. 12
0
 private static bool Greater(IComparable[] pq, int i, int j)
 {
     return(SortHelper.Greater(pq[i - 1], pq[j - 1]));
 }