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>
        /// 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);
        }