Пример #1
0
        //============================================= Partition ==========================================================
        protected static int Partition(List <TShirt> shirts, int low, int high, Check.comparison comparison)
        {
            TShirt pivot = shirts[high];

            // index of smaller element
            TShirt temp;
            int    i = (low - 1);

            for (int j = low; j < high; j++)
            {
                // If current element is smaller
                // than the pivot
                if (!comparison(shirts[j], pivot))
                {
                    i++;

                    // swap arr[i] and arr[j]
                    temp      = shirts[i];
                    shirts[i] = shirts[j];
                    shirts[j] = temp;
                }
            }

            // swap arr[i+1] and arr[high] (or pivot)
            TShirt temp1 = shirts[i + 1];

            shirts[i + 1] = shirts[high];
            shirts[high]  = temp1;

            return(i + 1);
        }
Пример #2
0
        //============================================= Quick Sort =========================================================
        public static void ExecuteQuickSort(List <TShirt> shirts, int low, int high, Check.comparison comparison)
        {
            if (low < high)
            {
                /* pi is partitioning index, arr[pi] is
                 * now at right place */
                int pi = Partition(shirts, low, high, comparison);

                // Recursively sort elements before
                // partition and after partition
                ExecuteQuickSort(shirts, low, pi - 1, comparison);
                ExecuteQuickSort(shirts, pi + 1, high, comparison);
            }
        }
Пример #3
0
        public static void ExecuteInsertionSort(List <TShirt> shirts, Check.comparison comparison)
        {
            int n = shirts.Count;

            for (int i = 1; i < n; ++i)
            {
                TShirt key = shirts[i];
                int    j   = i - 1;

                while (j >= 0 && comparison(shirts[j], key))
                {
                    shirts[j + 1] = shirts[j];
                    j             = j - 1;
                }
                shirts[j + 1] = key;
            }
        }
Пример #4
0
        public static void ExecuteBubbleSort(List <TShirt> shirts, Check.comparison comparison)
        {
            TShirt temporary;

            for (int p = 0; p <= shirts.Count - 2; p++)
            {
                for (int i = 0; i <= shirts.Count - 2; i++)
                {
                    if (comparison(shirts[i], shirts[i + 1]))
                    {
                        temporary     = shirts[i + 1];
                        shirts[i + 1] = shirts[i];
                        shirts[i]     = temporary;
                    }
                }
            }
        }
        public static void ExecuteBucketSort(List <TShirt> shirts, Check.comparison comparisonFirst, Check.comparison comparisonSecond, Check.comparison comparisonThird)
        {
            List <TShirt> result = new List <TShirt>();

            //Determine how many buckets you want to create, in this case, the 10 buckets will each contain a range of 10
            //1-10, 11-20, 21-30, etc. since the passed array is between 1 and 99
            int numOfBuckets = 10;

            //Create buckets
            List <List <TShirt> > buckets = new List <List <TShirt> >();

            for (int i = 0; i < numOfBuckets; i++)
            {
                buckets.Add(new List <TShirt>());
            }

            //Iterate through the passed array and add each integer to the appropriate bucket
            for (int i = 0; i < shirts.Count; i++)
            {
                int buckitChoice = ((int)shirts[i].fabric / numOfBuckets);
                buckets[buckitChoice].Add(shirts[i]);
            }

            //Sort each bucket and add it to the result List
            //Each sublist is sorted using Bubblesort, but you could substitute any sorting algo you would like
            for (int i = 0; i < numOfBuckets; i++)
            {
                InsertionSort.ExecuteInsertionSort(buckets[i], comparisonThird);
                InsertionSort.ExecuteInsertionSort(buckets[i], comparisonSecond);
                InsertionSort.ExecuteInsertionSort(buckets[i], comparisonFirst);

                result.AddRange(buckets[i]);
            }

            shirts.Clear();
            shirts.AddRange(result);
        }