Esempio n. 1
0
        /// <summary>
        /// Selection sorts the animal by name.
        /// </summary>
        /// <param name="animals">The animals in the list.</param>
        /// <returns>Returns the selection sort result.</returns>
        public static SortResult SelectionSortByName(List <Animal> animals)
        {
            SortResult sortResult = null;

            // initialize a swap counter variable.
            int swapCounter = 0;

            // initializes a compare counter variable.
            int compareCounter = 0;

            // initializes a stopwatch variable.
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            // loop forward through the list.
            for (int i = 0; i < animals.Count - 1; i++)
            {
                // declare a variable to hold the animal with the first name on the list.
                Animal firstAnimalName;

                // set the variable to the current animal.
                firstAnimalName = animals[i];

                // loop through the remaining animals in the list to find the animal that should be near the alphabetical top.
                for (int j = i + 1; j < animals.Count; j++)
                {
                    // Tracks how many times the animals are compared.
                    compareCounter++;

                    // if the name of the current animal is less than the name of the animal with the higher alphabetical standing,
                    // set the variable holding the animal with the current highest standing to the new current animal.
                    if (animals[j].Name.CompareTo(firstAnimalName.Name) < 0)
                    {
                        firstAnimalName = animals[j];
                    }
                }

                ////after finding the animal with the highest alphabetical standing,
                //// if the current animal's name does not equal the name of the animal with current name highest on the list,
                ////swap the two animals and increment the swap count
                if (animals[i].Name != firstAnimalName.Name)
                {
                    Swap(animals, i, animals.IndexOf(firstAnimalName));
                    swapCounter++;
                }
            }

            // Stops the stopwatch.
            stopwatch.Stop();

            // return the SortResult
            return(sortResult = new SortResult {
                SwapCount = swapCounter, Animals = animals, CompareCount = compareCounter, ElapsedMilliseconds = stopwatch.Elapsed.TotalMilliseconds
            });
        }
Esempio n. 2
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);
        }
Esempio n. 3
0
        /// <summary>
        /// Selection sorts the animal by weight.
        /// </summary>
        /// <param name="animals">The animals in the list.</param>
        /// <returns>Returns the selection sort result.</returns>
        public static SortResult SelectionSortByWeight(List <Animal> animals)
        {
            SortResult sortResult = null;

            // initialize a swap counter variable.
            int swapCounter = 0;

            // initializes a compare counter variable.
            int compareCounter = 0;

            // initializes a stopwatch variable.
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            // loop forward through the list.
            for (int i = 0; i < animals.Count - 1; i++)
            {
                // declare a variable to hold the animal with the current minimum weight.
                Animal minWeightAnimal;

                // set the variable to the current animal.
                minWeightAnimal = animals[i];

                // loop through the remaining animals in the list to find the animal with the lowest weight.
                for (int j = i + 1; j < animals.Count; j++)
                {
                    // Tracks how many times the animals are compared.
                    compareCounter++;

                    // if the weight of the current animal is less than the weight of the animal with the minimum weight, set the variable holding the animal with the current minimum weight to the current animal.
                    if (animals[j].Weight < minWeightAnimal.Weight)
                    {
                        minWeightAnimal = animals[j];
                    }
                }

                ////after finding the animal with the lowest weight
                //// if the current animal's weight does not equal the weight of the animal with current minimum weight,
                ////swap the two animals and increment the swap count
                if (animals[i].Weight != minWeightAnimal.Weight)
                {
                    Swap(animals, i, animals.IndexOf(minWeightAnimal));
                    swapCounter++;
                }
            }

            // Stops the stopwatch.
            stopwatch.Stop();

            // return the SortResult
            return(sortResult = new SortResult {
                SwapCount = swapCounter, Animals = animals, CompareCount = compareCounter, ElapsedMilliseconds = stopwatch.Elapsed.TotalMilliseconds
            });
        }
Esempio n. 4
0
        /// <summary>
        /// Sorts animals by name with a selection algorithm.
        /// </summary>
        /// <param name="animals">The list to be sorted through.</param>
        /// <returns>The resulting sort.</returns>
        public static SortResult SelectionSort(this IList list, Func <object, object, int> comparer)
        {
            // initialize a swap counter variable, smallest weight found, and stopwatch
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            int swapCounter    = 0;
            int minAnimalIndex = 0;

            // Initialize compare counter
            int compareCount = 0;

            // loop forward through the list
            for (int i = 0; i < list.Count - 1; i++)
            {
                // declare a variable to hold the animal with the current minimum weight
                // set the variable to the current animal
                Object minObject = list[i];

                // loop through the remaining animals in the list to find the animal with the lowest weight
                for (int j = i + 1; j < list.Count; j++)
                {
                    // if the weight of the current animal is less than the weight of the animal with the minimum weight,
                    // set the variable holding the animal with the current minimum weight to the current animal
                    if (comparer(list[j], minObject) == -1)
                    {
                        minObject      = list[j];
                        minAnimalIndex = j;
                    }
                }

                compareCount++;

                // after finding the animal with the lowest weight
                // if the current animal's weight does not equal the weight of the animal with
                // current minimum weight, swap the two animals and increment the swap count
                if (comparer(list[i], minObject) != 0)
                {
                    SortHelper.Swap(list, i, minAnimalIndex);
                    swapCounter++;
                }
            }

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

            // return the SortResult
            return(result);
        }
Esempio n. 5
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);
        }
        /// <summary>
        /// Uses a quick sort to sort list by name.
        /// </summary>
        /// <param name="list">The list of list to sort.</param>
        /// <param name="leftIndex">The leftmost part of the list to sort.</param>
        /// <param name="rightIndex">The rightmost part of the list to sort.</param>
        /// <param name="sortResult">The object holding the results of the sort algorithm.</param>
        /// <param name="comparer">The function used to sort the list.</param>
        public static void QuickSort(this IList list, int leftIndex, int rightIndex, SortResult sortResult, Func <object, object, int> comparer)
        {
            // initialize variables to the passed-in indexes
            int leftPointer  = leftIndex;
            int rightPointer = rightIndex;

            object pivotObject = list[(leftIndex + rightIndex) / 2];

            while (true)
            {
                while (comparer(list[leftPointer], pivotObject) < 0)
                {
                    leftPointer++;
                    sortResult.CompareCount++;
                }

                while (comparer(pivotObject, list[rightPointer]) < 0)
                {
                    rightPointer--;
                    sortResult.CompareCount++;
                }

                if (leftPointer <= rightPointer)
                {
                    list.Swap(leftPointer, rightPointer);
                    sortResult.SwapCount++;
                    leftPointer++;
                    rightPointer--;
                }

                if (leftPointer > rightPointer)
                {
                    break;
                }
            }

            if (leftIndex < rightPointer)
            {
                list.QuickSort(leftIndex, rightPointer, sortResult, comparer);
            }

            if (leftPointer < rightIndex)
            {
                list.QuickSort(leftPointer, rightIndex, sortResult, comparer);
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Bubble sorts the animal by name.
        /// </summary>
        /// <param name="animals">The animals in the list.</param>
        /// <returns>Returns the bubble sort result.</returns>
        public static SortResult BubbleSortByName(List <Animal> animals)
        {
            SortResult sortResult = null;

            // initialize a swap counter variable.
            int swapCounter = 0;

            // initializes a compare counter variable.
            int compareCounter = 0;

            // initializes a stopwatch variable.
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            // use a for loop to loop backward through the list.
            // e.g. initialize the loop variable to one less than the length of the list and decrement the variable instead of increment.
            for (int i = animals.Count - 1; i > 0; i--)
            {
                // loop forward as long as the loop variable is less than the outer loop variable.
                for (int j = 0; j < i; j++)
                {
                    // Tracks how many times the animals are compared.
                    compareCounter++;

                    // if the name of the current animal is more than the name of the next animal, swap the two animals and increment the swap count. If the first string is less than the second, a negative number is returned.  If the first string is greater, a positive number is returned.  If the strings are equal, the return value is zero.
                    int compareResult = string.Compare(animals[j].ToString(), animals[j + 1].ToString());

                    if (compareResult == 1)
                    {
                        Swap(animals, j, j + 1);
                        swapCounter++;
                    }
                }
            }

            // Stops the stopwatch.
            stopwatch.Stop();

            // return the SortResult
            return(sortResult = new SortResult {
                SwapCount = swapCounter, Animals = animals, CompareCount = compareCounter, ElapsedMilliseconds = stopwatch.Elapsed.TotalMilliseconds
            });
        }
Esempio n. 8
0
        /// <summary>
        /// Runs a bubble sorting algorithm by name.
        /// </summary>
        /// <param name="animals">The list of animals to be sorted through.</param>
        /// <returns>The sorted list.</returns>
        public static SortResult BubbleSort(this IList list, Func <object, object, int> comparer)
        {
            // initialize a swap counter variable and stopwatch
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            int swapCounter = 0;

            // Initialize compare counter
            int compareCount = 0;

            // use a for loop to loop backward through the list
            // e.g. initialize the loop variable to one less than the length of the list and decrement the variable instead of increment
            for (int i = list.Count - 1; i > 0; i--)
            {
                // loop forward as long as the loop variable is less than the outer loop variable
                for (int j = 0; j < i; j++)
                {
                    compareCount++;

                    // if the name of the current animal is more than the name of the next animal,
                    // swap the two animals and increment the swap count
                    if (comparer(list[j], list[j + 1]) > 0)
                    {
                        SortHelper.Swap(list, j, j + 1);
                        swapCounter++;
                    }
                }
            }

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

            // return the SortResult
            return(result);
        }
Esempio n. 9
0
        /// <summary>
        /// Bubble sorts the animal by weight.
        /// </summary>
        /// <param name="animals">The animals in the list.</param>
        /// <returns>Returns the bubble sort result.</returns>
        public static SortResult BubbleSortByWeight(List <Animal> animals)
        {
            SortResult sortResult = null;

            // initialize a swap counter variable.
            int swapCounter = 0;

            // initializes a compare counter variable.
            int compareCounter = 0;

            // initializes a stopwatch variable.
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            // use a for loop to loop backward through the list.
            // e.g. initialize the loop variable to one less than the length of the list and decrement the variable instead of increment.
            for (int i = animals.Count - 1; i > 0; i--)
            {
                // loop forward as long as the loop variable is less than the outer loop variable.
                for (int j = 0; j < i; j++)
                {
                    // Tracks how many times the animals are compared.
                    compareCounter++;

                    // if the weight of the current animal is more than the weight of the next animal, swap the two animals and increment the swap count.
                    if (animals[j].Weight > animals[j + 1].Weight)
                    {
                        Swap(animals, j, j + 1);
                        swapCounter++;
                    }
                }
            }

            // Stops the stopwatch.
            stopwatch.Stop();

            // return the SortResult.
            return(sortResult = new SortResult {
                SwapCount = swapCounter, Animals = animals, CompareCount = compareCounter, ElapsedMilliseconds = stopwatch.Elapsed.TotalMilliseconds
            });
        }
Esempio n. 10
0
        /// <summary>
        /// Insertion sorts the animal by name.
        /// </summary>
        /// <param name="animals">The animals in the list.</param>
        /// <returns>Returns the insertion sort result.</returns>
        public static SortResult InsertionSortByName(List <Animal> animals)
        {
            SortResult sortResult = null;

            // initialize a swap counter variable
            int swapCounter = 0;

            // initializes a compare counter variable.
            int compareCounter = 0;

            // initializes a stopwatch variable.
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            // loop forward through the list
            for (int i = 1; i < animals.Count; i++)
            {
                // Tracks how many times the animals are compared.
                compareCounter++;

                for (int j = i; j > 0; j--)
                {
                    if (animals[j].Name.CompareTo(animals[j - 1].Name) < 0)
                    {
                        // swap the current animal with the previous animal and increment the swap count
                        Swap(animals, j, j - 1);
                        swapCounter++;
                    }
                }
            }

            // Stops the stopwatch.
            stopwatch.Stop();

            // return the SortResult
            return(sortResult = new SortResult {
                SwapCount = swapCounter, Animals = animals, CompareCount = compareCounter, ElapsedMilliseconds = stopwatch.Elapsed.TotalMilliseconds
            });
        }
Esempio n. 11
0
        /// <summary>
        /// Sorts animals by name using an insertion algorithm.
        /// </summary>
        /// <param name="animals">List to be sorted.</param>
        /// <returns>The resulting sort.</returns>
        public static SortResult InsertionSort(this IList list, Func <object, object, int> comparer)
        {
            // initialize a swap counter variable and stopwatch
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            int swapCounter = 0;

            // Initialize compare counter
            int compareCount = 0;

            // loop forward through the list
            for (int i = 1; i < list.Count; i++)
            {
                compareCount++;

                // loop backward through the section of the list that has already been sorted to find the correct spot for the animal
                // loop while the value is greater than 0 and while the current animal's name is less than the previous animal's name
                for (int j = i; j > 0 && comparer(list[j], list[j - 1]) != 1; j--)
                {
                    SortHelper.Swap(list, j, j - 1);
                    swapCounter++;
                }
            }

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

            // return the SortResult
            return(result);
        }
Esempio n. 12
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);
        }
Esempio n. 13
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);
        }
Esempio n. 14
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 QuickSortByWeight(List <Animal> animals, int leftIndex, int rightIndex, SortResult sort)
        {
            // Variables to keep track of left and right points in list.
            int leftPointer  = leftIndex;
            int rightPointer = rightIndex;

            // Find the animal to pivot on (In this case, the midde.)
            Animal pivotAnimal = animals[(leftIndex + rightIndex) / 2];

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

            // start looping
            while (!done)
            {
                // While the weight of the animal at the left pointer spot in the list is less than the pivot animal's weight
                while (animals[leftPointer].Weight < pivotAnimal.Weight)
                {
                    // increment the left pointer
                    leftPointer++;

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

                // While the pivot animal's weight is less than the weight of the animal at the right pointer spot in the list
                while (pivotAnimal.Weight < animals[rightPointer].Weight)
                {
                    // decrement the right pointer
                    rightPointer--;

                    // increment the sort result's compare count
                    sort.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
                    sort.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)
            {
                QuickSortByWeight(animals, leftIndex, rightPointer, sort);
            }

            // 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)
            {
                QuickSortByWeight(animals, leftPointer, rightIndex, sort);
            }

            // Set the sorts Animal's property to the sorted list of animals.
            sort.Animals = animals;

            // Return the sort?
            return(sort);
        }
Esempio n. 15
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);
        }
Esempio n. 16
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);
        }
Esempio n. 17
0
        /// <summary>
        /// Quick sorts the animals by name.
        /// </summary>
        /// <param name="animals">The list of animals to sort.</param>
        /// <param name="leftIndex">The left index number.</param>
        /// <param name="rightIndex">The right index number.</param>
        /// <param name="sortResult">The resulting sort. </param>
        /// <returns>The sort result.</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 not equal to the pivot animal's name
                // increment the left pointer
                // increment the sort result's compare count
                while (animals[leftPointer].Name.CompareTo(pivotAnimal.Name) < 0)
                {
                    leftPointer++;
                    sortResult.CompareCount++;
                }

                //// while the pivot animal's name is less than the name of the animal at the right pointer spot in the list
                //// decrement the right pointer
                //// increment the sort result's compare count
                while (pivotAnimal.Name.CompareTo(animals[rightPointer].Name) < 0)
                {
                    rightPointer--;
                    sortResult.CompareCount++;
                }

                //// if the left pointer is less than or equal to the right pointer
                //// swap the animals at the left pointer and right pointer spots
                //// increment the sort result's swap count
                //// then increment the left pointer and decrement the right pointer

                if (leftPointer <= rightPointer)
                {
                    Swap(animals, leftPointer, rightPointer);
                    sortResult.SwapCount++;
                    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)
            {
                // method call here
                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)
            {
                // method call here
                QuickSortByName(animals, leftPointer, rightIndex, sortResult);
            }

            // Set the animals parameter to the SortResult's Animals property before returning the SortResult variable.
            sortResult.Animals = animals;

            // return the SortResult
            return(sortResult);
        }