Example #1
0
        /// <summary>
        /// Sorts zoo animals.
        /// </summary>
        /// <param name="sortType">The type of sorting algorithm to be used.</param>
        /// <param name="sortValue">The value to be sorted with.</param>
        /// <returns>A sorted list.</returns>
        private SortResult SortObjects(string sortType, string sortValue, IList list)
        {
            Func <object, object, int> sortFunc;

            if (sortValue == "animalname")
            {
                sortFunc = AnimalNameSortComparer;
            }
            else if (sortValue == "guestname")
            {
                sortFunc = GuestNameSortComparer;
            }
            else if (sortValue == "weight")
            {
                sortFunc = WeightSortComparer;
            }
            else if (sortValue == "age")
            {
                sortFunc = AgeSortComparer;
            }
            else
            {
                sortFunc = MoneyBalanceSortComparer;
            }

            SortResult result = new SortResult();

            switch (sortType)
            {
            case "bubble":
                result = SortHelper.BubbleSort(list, sortFunc);
                break;

            case "selection":
                result = SortHelper.SelectionSort(list, sortFunc);
                break;

            case "insertion":
                result = SortHelper.InsertionSort(list, sortFunc);
                break;

            case "quick":
                if (sortValue == "weight")
                {
                    result = SortHelper.QuickSort(list, 0, list.Count - 1, result, sortFunc);
                }
                break;
            }

            return(result);
        }
Example #2
0
        /// <summary>
        /// Uses a quick sort algorithm to sort by name.
        /// </summary>
        /// <param name="animals">The animals to be sorted.</param>
        /// <param name="leftIndex">The lowest index of the list.</param>
        /// <param name="rightIndex">The highest index of the list.</param>
        /// <param name="sortResult">The resulting sort.</param>
        /// <returns></returns>
        public static SortResult QuickSort(this IList list, int leftIndex, int rightIndex, SortResult sortResult, Func <object, object, int> comparer)
        {
            // define variables to keep track of the left and right points in the list
            // initialize them to the passed-in indexes
            int leftPointer  = leftIndex;
            int rightPointer = rightIndex;

            // initialize a swap counter variable and stopwatch
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            int swapCounter = 0;

            // Initialize compare counter
            int compareCount = 0;

            // find the animal to pivot on (the middle animal in this case)
            Object pivotObject = list[(leftIndex + rightIndex) / 2];

            // define and initialize a loop variable
            bool done = false;

            // start looping
            while (!done)
            {
                // while the name of the animal at the left pointer spot in the list is less than the pivot animal's name
                while (comparer(list[leftPointer], pivotObject) < 0)
                {
                    // increment the left pointer
                    leftPointer++;
                    // increment the sort result's compare count
                    compareCount++;
                }

                // while the pivot animal's name is less than the name of the animal at the right pointer spot in the list
                while (comparer(pivotObject, list[rightPointer]) > 0)
                {
                    // decrement right pointer
                    rightPointer--;
                    // increment the sort result's compare count
                    compareCount++;
                }

                // if the left pointer is less than or equal to the right pointer
                if (leftPointer <= rightPointer)
                {
                    // swap the animals at the left pointer and right pointer spots
                    SortHelper.Swap(list, leftPointer, rightPointer);

                    // increment the sort result's swap count
                    swapCounter++;
                    // then increment the left pointer and decrement the right pointer
                    leftPointer++;
                    rightPointer--;
                }
                // if the left pointer is greater than the right pointer,
                // stop the outer while loop
                if (leftPointer > rightPointer)
                {
                    done = true;
                }
            }

            // if the left index is less than the right pointer
            // use recursion to sort the animals within the left index and right pointer
            if (leftIndex < rightPointer)
            {
                SortHelper.QuickSort(list, leftIndex, rightPointer, sortResult, comparer);
            }

            // if the left pointer is less than the right index
            // use recursion to sort the animals within the left pointer and right index
            if (leftPointer < rightIndex)
            {
                SortHelper.QuickSort(list, leftPointer, rightIndex, sortResult, comparer);
            }


            stopwatch.Stop();
            SortResult result = new SortResult
            {
                Objects             = list.Cast <object>().ToList(),
                SwapCount           = swapCounter,
                CompareCount        = compareCount,
                ElapsedMilliseconds = stopwatch.ElapsedMilliseconds
            };

            // return the SortResult
            return(result);
        }
Example #3
0
        /// <summary>
        /// Sorts the zoo's list of animals.
        /// </summary>
        /// <param name="sortType">The type of sort to perform.</param>
        /// <param name="sortValue">The value on which to sort.</param>
        /// <param name="list">The list to be sorted.</param>
        /// <returns>The result of sorting (number of swaps and number of comparisons).</returns>
        public SortResult SortObjects(SortType sortType, string sortValue, IList list)
        {
            SortResult result = null;

            Func <object, object, int> comparer;

            switch (sortType)
            {
            case SortType.Bubble:
                if (sortValue == "animalName")
                {
                    comparer = AnimalNameSortComparer;
                    result   = SortHelper.BubbleSort(list, comparer);
                }
                else if (sortValue == "guestName")
                {
                    comparer = GuestNameSortComparer;
                    result   = SortHelper.BubbleSort(list, comparer);
                }
                else if (sortValue == "moneyBalance")
                {
                    comparer = MoneyBalanceSortComparer;
                    result   = SortHelper.BubbleSort(list, comparer);
                }
                else if (sortValue == "age")
                {
                    comparer = AgeSortComparer;
                    result   = SortHelper.BubbleSort(list, comparer);
                }
                else
                {
                    comparer = WeightSortComparer;
                    result   = SortHelper.BubbleSort(list, comparer);
                }

                break;

            case SortType.Selection:
                if (sortValue == "animalName")
                {
                    comparer = AnimalNameSortComparer;
                    result   = SortHelper.SelectionSort(list, comparer);
                }
                else if (sortValue == "guestName")
                {
                    comparer = GuestNameSortComparer;
                    result   = SortHelper.SelectionSort(list, comparer);
                }
                else if (sortValue == "moneyBalance")
                {
                    comparer = MoneyBalanceSortComparer;
                    result   = SortHelper.SelectionSort(list, comparer);
                }
                else if (sortValue == "age")
                {
                    comparer = AgeSortComparer;
                    result   = SortHelper.SelectionSort(list, comparer);
                }
                else
                {
                    comparer = WeightSortComparer;
                    result   = SortHelper.SelectionSort(list, comparer);
                }

                break;

            case SortType.Insertion:
                if (sortValue == "animalName")
                {
                    comparer = AnimalNameSortComparer;
                    result   = SortHelper.InsertionSort(list, comparer);
                }
                else if (sortValue == "guestName")
                {
                    comparer = GuestNameSortComparer;
                    result   = SortHelper.InsertionSort(list, comparer);
                }
                else if (sortValue == "moneyBalance")
                {
                    comparer = MoneyBalanceSortComparer;
                    result   = SortHelper.InsertionSort(list, comparer);
                }
                else if (sortValue == "age")
                {
                    comparer = AgeSortComparer;
                    result   = SortHelper.InsertionSort(list, comparer);
                }
                else
                {
                    comparer = WeightSortComparer;
                    result   = SortHelper.InsertionSort(list, comparer);
                }

                break;

            case SortType.Shell:
                if (sortValue == "animalName")
                {
                    comparer = AnimalNameSortComparer;
                    result   = SortHelper.ShellSort(list, comparer);
                }
                else if (sortValue == "guestName")
                {
                    comparer = GuestNameSortComparer;
                    result   = SortHelper.ShellSort(list, comparer);
                }
                else if (sortValue == "moneyBalance")
                {
                    comparer = MoneyBalanceSortComparer;
                    result   = SortHelper.ShellSort(list, comparer);
                }
                else if (sortValue == "age")
                {
                    comparer = AgeSortComparer;
                    result   = SortHelper.ShellSort(list, comparer);
                }
                else
                {
                    comparer = WeightSortComparer;
                    result   = SortHelper.ShellSort(list, comparer);
                }

                break;

            case SortType.Quick:
                Stopwatch sw = new Stopwatch();
                sw.Start();
                SortResult sortResult = new SortResult();

                if (sortValue == "animalName")
                {
                    comparer = AnimalNameSortComparer;
                    SortHelper.QuickSort(list, 0, list.Count - 1, sortResult, comparer);
                }
                else if (sortValue == "guestName")
                {
                    comparer = GuestNameSortComparer;
                    SortHelper.QuickSort(list, 0, list.Count - 1, sortResult, comparer);
                }
                else if (sortValue == "moneyBalance")
                {
                    comparer = MoneyBalanceSortComparer;
                    SortHelper.QuickSort(list, 0, list.Count - 1, sortResult, comparer);
                }
                else if (sortValue == "age")
                {
                    comparer = AgeSortComparer;
                    SortHelper.QuickSort(list, 0, list.Count - 1, sortResult, comparer);
                }
                else
                {
                    comparer = WeightSortComparer;
                    SortHelper.QuickSort(list, 0, list.Count - 1, sortResult, comparer);
                }

                sw.Stop();
                sortResult.ElapsedMilliseconds = sw.Elapsed.TotalMilliseconds;

                result = sortResult;
                break;

            default:
                break;
            }

            return(result);
        }