コード例 #1
0
        private static void ThreeWaySort <T>(IList <T> collection, int lo, int hi, IComparer <T> comparer)
        {
            if (hi <= lo)
            {
                return;
            }

            int lt = lo;
            int i  = lo + 1;
            int gt = hi;

            var v = collection[lo];

            while (i <= gt)
            {
                int cmp = comparer.Compare(collection[i], v);
                if (cmp < 0)
                {
                    SortingBase.Exch(collection, lt++, i++);
                }
                else if (cmp > 0)
                {
                    SortingBase.Exch(collection, i, gt--);
                }
                else
                {
                    i++;
                }
            }
            Sort(collection, lo, lt - 1, comparer);
            Sort(collection, gt + 1, hi, comparer);
        }
コード例 #2
0
        private static int Partition <T>(IList <T> collection, int lo, int hi, IComparer <T> comparer)
        {
            int i = lo;
            int j = hi + 1;

            var v = collection[lo];

            while (true)
            {
                while (SortingBase.Less(collection[++i], v, comparer))
                {
                    if (i == hi)
                    {
                        break;
                    }
                }

                while (SortingBase.Less(v, collection[--j], comparer))
                {
                    if (j == lo)
                    {
                        break;
                    }
                }

                if (i >= j)
                {
                    break;
                }

                SortingBase.Exch(collection, i, j);
            }
            SortingBase.Exch(collection, lo, j);
            return(j);
        }
コード例 #3
0
        private static void Merge <T>(IList <T> collection, int lo, int mid, int hi, T[] aux, IComparer <T> comparer)
        {
            Debug.Assert(IsSorted(collection, lo, mid, comparer));
            Debug.Assert(IsSorted(collection, mid + 1, hi, comparer));

            int i = lo;
            int j = mid + 1;

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

            for (int k = lo; k <= hi; k++)
            {
                if (i > mid)
                {
                    collection[k] = aux[j++];
                }
                else if (j > hi)
                {
                    collection[k] = aux[i++];
                }
                else if (SortingBase.Less(aux[j], aux[i], comparer))
                {
                    collection[k] = aux[j++];
                }
                else
                {
                    collection[k] = aux[i++];
                }
            }
        }
コード例 #4
0
 private static bool IsSorted <T>(IList <T> collection, int lo, int hi, IComparer <T> comparer)
 {
     for (int i = lo + 1; i <= hi; i++)
     {
         if (SortingBase.Less(collection[i], collection[i - 1], comparer))
         {
             return(false);
         }
     }
     return(true);
 }
コード例 #5
0
        /// <summary>
        /// Sort the List
        /// </summary>
        /// <typeparam name="T">the type of the stored values</typeparam>
        /// <param name="collection">the collection to sort</param>
        /// <param name="comparer">the comparer for the values, if not specified the default comparer is used</param>
        public static void InsertionSort <T>(this IList <T> collection, IComparer <T> comparer = null)
        {
            comparer ??= Comparer <T> .Default;

            int n = collection.Count;

            for (int i = 1; i < n; i++)
            {
                for (int j = i; j > 0 && SortingBase.Less((T)collection[j], (T)collection[j - 1], comparer); j--)
                {
                    SortingBase.Exch(collection, j, j - 1);
                }
            }
        }
コード例 #6
0
        /// <summary>
        /// Sort the List
        /// </summary>
        /// <typeparam name="T">the type of the stored values</typeparam>
        /// <param name="collection">the collection to sort</param>
        /// <param name="comparer">the comparer for the values, if not specified the default comparer is used</param>
        public static void SelectionSort <T>(this IList <T> collection, IComparer <T> comparer = null)
        {
            comparer ??= Comparer <T> .Default;
            int n = collection.Count;

            for (int i = 0; i < n; i++)
            {
                int min = i;
                for (int j = i + 1; j < n; j++)
                {
                    if (SortingBase.Less(collection[j], collection[min], comparer))
                    {
                        min = j;
                    }
                }
                SortingBase.Exch(collection, i, min);
            }
        }
コード例 #7
0
ファイル: ShellSort.cs プロジェクト: PizzaServices/Algorithms
        /// <summary>
        /// Sort the List
        /// </summary>
        /// <typeparam name="T">the type of the stored values</typeparam>
        /// <param name="collection">the collection to sort</param>
        /// <param name="comparer">the comparer for the values, if not specified the default comparer is used</param>
        public static void ShellSort <T>(this IList <T> collection, IComparer <T> comparer = null)
        {
            comparer ??= Comparer <T> .Default;

            int n = collection.Count;
            int h = 1;

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

            while (h >= 1)
            {
                for (int i = h; i < n; i++)
                {
                    for (int j = i; j >= h && SortingBase.Less((T)collection[j], (T)collection[j - h], comparer); j -= h)
                    {
                        SortingBase.Exch(collection, j, j - h);
                    }
                }
                h /= 3;
            }
        }