Example #1
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;
            }

            return(result);
        }
Example #2
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);
        }
Example #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);
        }