- The console and zoo expose the structures needed to request and report a sort.
NewZoo method and put the following code in the Zoo's constructor to initialize the list of animals this.animals = new List<Animal>()
{
new Chimpanzee("Bobo", 10, 128.2, Gender.Male),
new Chimpanzee("Bubbles", 3, 103.8, Gender.Female),
new Dingo("Spot", 5, 41.3, Gender.Male),
new Dingo("Maggie", 6, 37.2, Gender.Female),
new Dingo("Toby", 0, 15.0, Gender.Male),
new Eagle("Ari", 12, 10.1, Gender.Female),
new Hummingbird("Buzz", 2, 0.02, Gender.Male),
new Hummingbird("Bitsy", 1, 0.03, Gender.Female),
new Kangaroo("Kanga", 8, 72.0, Gender.Female),
new Kangaroo("Roo", 0, 23.9, Gender.Male),
new Kangaroo("Jake", 9,153.5, Gender.Male),
new Ostrich("Stretch", 26, 231.7, Gender.Male),
new Ostrich("Speedy", 30, 213.0, Gender.Female),
new Platypus("Patti", 13, 4.4, Gender.Female),
new Platypus("Bill", 11, 4.9, Gender.Male),
new Platypus("Ted", 0, 1.1, Gender.Male),
new Shark("Bruce", 19, 810.6, Gender.Female),
new Shark("Anchor", 17, 458.0, Gender.Male),
new Shark("Chum", 14, 377.3, Gender.Male),
new Squirrel("Chip", 4, 1.0, Gender.Male),
new Squirrel("Dale", 4, 0.9, Gender.Male)
};Look at the diagram below to understand how the command structure will work.

Program class add a "sort" command to the Main's switch-statement.Zoo class and add the SortHelper and SortResult classes to the Zoos project, as specified in the diagram below.Return null in methods that have a return for now.

"sort" command call the Zoo's SortAnimals method passing in the second and third items in the commandWords array and catch the return in a SortResult type variable. (The command will look like: sort bubble weight)"sort" command to catch exceptions that may occur when entering in the commands.Write the following message to the console when an exception is caught:
Sort command must be entered as: sort [sort type] [sort by -- weight or name].SortAnimals method in the try, write the sortType and the sortValue to the console as seen below"SORT TYPE: " + commandWords[1].ToUpper()
"SORT BY: " + commandWords[2].ToUpper()SortResult's Swap Count property to write to the console how many times the animals are swapped when they are being sorted.It should look similar to below:
"SWAP COUNT: " + [SortResult variable here].SwapCount"SortResult variable's Animals property then write the animal to the console using the animal's ToString method.SortAnimals method, define a SortResult result variable and initialize it to null.sortType parameter."bubble" case and call the BubbleSortByWeight method, passing in the animals list.BubbleSortByWeight only if the sortValue is "weight".SortResult variable.SortHelper's Swap method, swap the two animals at the passed-in positions in the list.For example, if the values of the index parameters are 2 and 6, then the animal in the 6 position in the list should move to the 2 position, and the animal in the 2 position should move to the 6 position.
SortHelper's BubbleSortByWeight method. Run the console application."sort bubble weight" in the console. Ensure that the breakpoint is hit.Bubble Sort: The bubble sort loops through a list. With each loop it compares two adjacent values; if the values are out of order it will swap the two values. This forces larger values to "bubble" up to the end of the list and then stops the value from being compared to again.

By Swfung8 (Own work) [CC BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0) or GFDL (http://www.gnu.org/copyleft/fdl.html)], via Wikimedia Commons
BubbleSortByWeight method.int swapCounter = 0;
// use a for loop to loop backward through the list
for (int i = animals.Count - 1; i > 0; i--)
{
Console.WriteLine(i);
}SortResult value, set its SwapCount to the value of the swap counter variable and the Animals property to the animals that were sorted.Console and type 'sort bubble weight'.Your output should look like the image below. The list of animals has 21 animals in it so the starting number is 20 and it decrements with each loop.

Console.WriteLine() in the for loop.// loop forward as long as the loop variable is less than the outer loop variable
for (int j = 0; j < i; j++)
{
}"sort bubble weight".The outer loop should begin at 20 and the inner loop should begin at 0. While the outer loop is at 20, the inner loop will execute until it reaches 20 and then stops.
If it does, Swap the animals, passing in the animals list, j, and j + 1.
Swap, increase the swapCounter variable by 1.Console.WriteLine(i)"sort bubble weight".'Add watch'. This will show the animals list.It should be 128.2 (the weight of the first animal in the list, Bobo).
It should be 103.8 (the weight of the second animal in the list, Bubbles).
Swap method. Bubbles should now be at index 0 and Bobo should be at index 1.
Hover over the Weight properties of the logic check. The first animal should be 128.2 (Bobo) and the second animal should be 41.3 (Spot).
Swap call. Bobo and Spot should switch indexes. Check your Watch window for this.Bobo should continue to "bubble up" until he hits Jake and then the logic check will fail.
He has "bubbled" all the way to the top because he is the heaviest.
Now that we know Bruce is the heaviest animal, we don't care about him and the outer loop decreases to 19.
Your console should look like the output below with the animals sorted in ascending weights and 108 swaps made.

BubbleSortByName method to the SortHelper class as specified in the diagram below.
SortAnimals method, call the BubbleSortByName method in the "bubble" case if the sortValue is "name".It is the same as the algorithm for weight, but it compares the name values instead of the weight values.
5.3.4 How to Compare StringsBubbleSortByWeight method into the BubbleSortByName method.string.Compare()Compare method, pass in the animal's Name at j and the animal's Name at one more than j.string.Compare is greater than 0."sort bubble name."The animals should be sorted by name in ascending order and the swap count should be 108.
Selection Sort: The selection sort is similar to the bubble sort but improves upon it by making a maximum of one swap on each pass through the list instead of multiple swaps. The sort's outer loop looks at each item in the list to find out if an item has the minimum value of the unsorted items; if it doesn't have the minimum value, it will be swapped with whatever item does have the minimum value.
Sonny Jr via http://sonnyjr.me/tag/selection-sort/

SelectionSortByWeight method to the SortHelper as specified in the diagram below.
SortAnimals method, call the SelectionSortByWeight method in the "selection" case if the sortValue is "weight".SelectionSortByWeight codeswapCounter variable and initialize it to 0.Count of the animals list - 1, and increments by 1.Count of the animals list, and increments by 1.'sort selection weight'.Hummingbird and the i value should be 1.swapCounter by 1.'sort selection weight'.Swap method. Buzz should now be at index 0 and Bobo should be at index 6 (where Buzz was).The animals should be in ascending order by weight with a swap count of 16.

SelectionSortByName method to the SortHelper as specified in the diagram below.
SortAnimals method, call the SelectionSortByName method in the "selection" case if the sortValue is "name".It is the same as the algorithm for weight, but it compares the name values instead of the weight values.
"sort selection name." The animals should be sorted by name in ascending order.The swap count should be 20.
Zoo class' SortAnimals methodConsole Application© 2026 Northcentral Technical College