Exemple #1
0
        public static void QuickSort <T>(this IList <T> items)
            where T : IComparable <T>
        {
            var stats = new SortStats();

            QuickSort(items, 0, items.Count - 1, stats);
            Console.WriteLine("QuickSort: {0}", stats);
        }
Exemple #2
0
        public static void MergeSortInPlace <T>(IList <T> input)
            where T : IComparable <T>
        {
            SortStats ss = new SortStats();

            T[] scratch = new T[input.Count];
            MergeSortInPlace(input, 0, input.Count - 1, ss, scratch);
            Console.WriteLine("Mergesort: {0}", ss);
        }
Exemple #3
0
 static void QuickSort <T>(IList <T> A, int p, int r, SortStats ss)
     where T : IComparable <T>
 {
     if (p < r)
     {
         int q = Partition(A, p, r, ss);
         QuickSort(A, p, q - 1, ss);
         QuickSort(A, q + 1, r, ss);
     }
 }
Exemple #4
0
 static void InsertionSort <T>(IList <T> items, int p, int r, SortStats ss)
     where T : IComparable <T>
 {
     for (int i = p; i < r; i++)
     {
         T   x = items[i];
         int j = i - 1;
         while (j >= 0 && items[j].CompareTo(x) > 0)
         {
             ss.Compares++;
             items[j + 1] = items[j];
             j--;
             ss.Swaps++;
         }
         items[j + 1] = x;
     }
 }
Exemple #5
0
        static int Partition <T>(IList <T> A, int p, int r, SortStats ss)
            where T : IComparable <T>
        {
            T   x = A[r];
            int i = p - 1;

            for (int j = p; j < r; ++j)
            {
                ss.Compares++;
                if (A[j].CompareTo(x) <= 0)
                {
                    ss.Swaps++;
                    Swap(A, ++i, j);
                }
            }
            ss.Swaps++;
            Swap(A, ++i, r);
            return(i);
        }
Exemple #6
0
        private static void MergeInPlace <T>(IList <T> input, int startA, int startB, int endB, SortStats ss, T[] scratch)
            where T : IComparable <T>
        {
            var c = new T[endB - startA + 1];
            int ap = startA, bp = startB, cp = 0;

            while (cp < endB - startA + 1)
            {
                ss.Compares++;
                if (bp > endB || (ap <= (startB - 1) && (input[ap].CompareTo(input[bp]) < 0)))
                {
                    c[cp] = input[ap];
                    ap++;
                }
                else
                {
                    c[cp] = input[bp];
                    bp++;
                }
                cp++;
            }

            for (int i = startA; i <= endB; i++)
            {
                input[i] = c[i - startA];
            }
        }
Exemple #7
0
        public static void MergeSortInPlace <T>(IList <T> input, int startIndex, int endIndex, SortStats ss, T[] scratch)
            where T : IComparable <T>
        {
            if (endIndex - startIndex <= 0)
            {
                return;
            }
            int half = (endIndex - startIndex + 1) / 2;

            MergeSortInPlace(input, startIndex, startIndex + half - 1, ss, scratch);
            MergeSortInPlace(input, startIndex + half, endIndex, ss, scratch);
            MergeInPlace(input, startIndex, startIndex + half, endIndex, ss, scratch);
        }