Beispiel #1
0
        /// <summary>
        /// Sort the animals according to command type.
        /// </summary>
        /// <param name="sortType">The sort type to be executed.</param>
        /// <param name="sortValue">The number of times the sort was performed to achieve success.</param>
        /// <returns>Returns the sort result.</returns>
        public SortResult SortAnimals(string sortType, string sortValue)
        {
            SortResult sortResult = null;

            switch (sortType)
            {
            case "bubble":
                if (sortValue == "weight")
                {
                    sortResult = SortHelper.BubbleSortByWeight(this.animals);
                }

                if (sortValue == "name")
                {
                    sortResult = SortHelper.BubbleSortByName(this.animals);
                }

                break;

            case "selection":
                if (sortValue == "weight")
                {
                    sortResult = SortHelper.SelectionSortByWeight(this.animals);
                }

                if (sortValue == "name")
                {
                    sortResult = SortHelper.SelectionSortByName(this.animals);
                }

                break;

            case "insertion":
                if (sortValue == "weight")
                {
                    sortResult = SortHelper.InsertionSortByWeight(this.animals);
                }

                if (sortValue == "name")
                {
                    sortResult = SortHelper.InsertionSortByName(this.animals);
                }

                break;

            case "quick":
                if (sortValue == "weight")
                {
                    sortResult = new SortResult();
                    Stopwatch stopwatch = new Stopwatch();
                    stopwatch.Start();

                    // Call the QuickSortByWeight method.
                    sortResult = SortHelper.QuickSortByWeight(this.animals, this.animals.IndexOf(this.animals[0]), this.animals.LastIndexOf(this.animals[this.animals.Count - 1]), sortResult);
                    stopwatch.Stop();
                    sortResult.ElapsedMilliseconds = stopwatch.Elapsed.TotalMilliseconds;
                }

                if (sortValue == "name")
                {
                    sortResult = new SortResult();
                    Stopwatch stopwatch = new Stopwatch();
                    stopwatch.Start();

                    // Call the QuickSortByName method.
                    sortResult = SortHelper.QuickSortByName(this.animals, this.animals.IndexOf(this.animals[0]), this.animals.LastIndexOf(this.animals[this.animals.Count - 1]), sortResult);
                    stopwatch.Stop();
                    sortResult.ElapsedMilliseconds = stopwatch.Elapsed.TotalMilliseconds;
                }

                break;
            }

            return(sortResult);
        }
Beispiel #2
0
        /// <summary>
        /// The method that will do a quick sort.
        /// </summary>
        /// <param name="animals"> The animals being sorted.</param>
        /// <param name="leftIndex"> The value for the left.</param>
        /// <param name="rightIndex"> The value for the right.</param>
        /// <param name="sort"> The sort being returned.</param>
        /// <returns> The sort result by weight.</returns>
        public static SortResult QuickSortByName(List <Animal> animals, int leftIndex, int rightIndex, SortResult sortResult)
        {
            // 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;

            // find the animal to pivot on (the middle animal in this case)
            Animal pivotAnimal = animals[(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 (string.Compare(pivotAnimal.Name, animals[leftPointer].Name) == 1)
                {
                    // increment the left pointer
                    leftPointer++;

                    // increment the sort result's compare count
                    sortResult.CompareCount++;
                }

                // while the pivot animal's weight is less than the weight of the animal at the right pointer spot in the list
                while (string.Compare(pivotAnimal.Name, animals[rightPointer].Name) == -1)
                {
                    // decrement the right pointer
                    rightPointer--;

                    // increment the sort result's compare count
                    sortResult.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
                    Swap(animals, leftPointer, rightPointer);

                    // increment the sort result's swap count
                    sortResult.SwapCount++;

                    // 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.QuickSortByName(animals, leftIndex, rightPointer, sortResult);
            }

            // 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.QuickSortByName(animals, leftPointer, rightIndex, sortResult);
            }

            // Set the passed in animals parameter to the sort result's parameter.
            sortResult.Animals = animals;

            return(sortResult);
        }
Beispiel #3
0
        /// <summary>
        /// Sorts the animals in the list of animals.
        /// </summary>
        /// <param name="sortType"> Sorts the list of animals by this type.</param>
        /// <param name="sortValue"> Sorts the list of animals by this value.</param>
        /// <returns> The results of the sort.</returns>
        public SortResult SortAnimals(string sortType, string sortValue)
        {
            // Define variable of type SortResult and set to null.
            SortResult result = null;

            // Switch on the type of sort entered.
            switch (sortType)
            {
            // If "bubble" was entered, call the SortHelper's bubblesort method and give it the list of aniamls.
            case "bubble":
                if (sortValue == "weight")
                {
                    // Sort the animals by weight.
                    result = SortHelper.BubbleSortByWeight(this.animals);
                }
                if (sortValue == "name")
                {
                    // Sort the animals by weight.
                    result = SortHelper.BubbleSortByName(this.animals);
                }
                break;

            // If selection was typed then sort by selection.
            case "selection":
                // If you want to sort by the weight value then type weight.
                if (sortValue == "weight")
                {
                    // Sort the animals by weight using a Selection sort.
                    result = SortHelper.SelectionSortByWeight(this.animals);
                }
                if (sortValue == "name")
                {
                    // Sort the animals by their name.
                    result = SortHelper.SelectionSortByName(this.animals);
                }
                break;

            // If you want to sory by inesertion...
            case "insertion":
                // If you want to sort by the weight value then type weight.
                if (sortValue == "weight")
                {
                    // Sort by the animals weight and insertion.
                    result = SortHelper.InsertionSortByWeight(this.animals);
                }
                // If you want to sort by the name value then type name.
                if (sortValue == "name")
                {
                    // Sort by the animals weight and insertion.
                    result = SortHelper.InsertionSortByName(this.animals);
                }
                break;

            // If you want to sort by quick...
            case "quick":

                // Make a new sort result.
                SortResult sort = new SortResult();

                // Make a new stop watch and start the timer.
                Stopwatch watch = new Stopwatch();
                watch.Start();

                // If you want to sort by weight.
                if (sortValue == "name")
                {
                    // Pass in the lowest left index (0), and the highest right index (the number of items in the list)
                    SortHelper.QuickSortByName(this.animals, 0, this.animals.Count - 1, sort);

                    // Stop the watch.
                    watch.Stop();

                    // Set the sorts elapsed milliseconds to the watch's total elapsed milliseconds property.
                    sort.ElapsedMilliseconds = watch.Elapsed.TotalMilliseconds;

                    // Set the result variable to the sort resutl
                    result = sort;
                }

                // If you want to sort by weight.
                if (sortValue == "weight")
                {
                    // Pass in the lowest left index (0), and the highest right index (the number of items in the list)
                    SortHelper.QuickSortByWeight(this.animals, 0, this.animals.Count - 1, sort);

                    // Stop the watch.
                    watch.Stop();

                    // Set the sorts elapsed milliseconds to the watch's total elapsed milliseconds property.
                    sort.ElapsedMilliseconds = watch.Elapsed.TotalMilliseconds;

                    // Set the result variable to the sort resutl
                    result = sort;
                }
                break;
            }

            return(result);
        }
Beispiel #4
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);
        }