- Insertion sort orders animals by weight.
Insertion Sort: The insertion sort builds a sorted list from the start of the unsorted list. It loops through the remaining unsorted items in the list; each time through, it grabs the next unsorted item, and then loops back through the sorted part of the list to find the correct spot for the current unsorted item. After it finds the correct spot, it "inserts" the item into that spot.

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
InsertionSortByWeight method to the SortHelper as specified in the diagram below.
SortAnimals method, call the InsertionSortByWeight method in the "insertion" case if the sortValue is "weight".swapCounter and return a SortResult as done in the previous algorithms.swapCounter by 1.'sort insertion weight'.'step into' the inner for loop.The value is greater than 0 and the second animal in the list's Weight (103.8) is less than the first animal's (128.2) so the loop will execute.
Bobo and Bubbles should swap index positions in the list.
The value of the inner for loop becomes 0 (the beginning of the list) so the condition fails and you should be brought back to the outer loop.
'Step into' the inner for loop again.This time the value should be 2 and the second animal in the unsorted list is 41.3 and the first is 128.2.
This time the value is greater than 0 AND the Weight of the second animal (41.3) is less than the first animal (103.8) so the loop executes again and Spot should swap with Bubbles. The loop should break because it reaches the beginning of the list.
"inserted" into their proper place.The animals should be sorted in ascending order by weight with a swap count of 108.
InsertionSortByName method to the SortHelper as specified in the diagram below.
SortAnimals method, call the InsertionSortByName method in the "insertion" 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 insertion name." The animals should be sorted by name in ascending order. The swap count should be 108.SortResult class in the Zoos project as specified in the diagram below.
"sort" case, write to the console underneath the swap count, the sortResult's CompareCount and the ElapsedMilliseconds.It should look like the following:
"COMPARE COUNT: " + [SortResult variable].CompareCount
"TIME: " + [SortResult variable].ElapsedMillisecondsCompareCount property in the object initializer.Stopwatch variable (in the System.Diagnostics namespace).Start the stopwatch at the beginning of the method, and stop it at the end of the method. Assign the ElapsedMilliseconds property in the object initialzer to the Stopwatch's Elapsed.TotalMilliseconds property.
"sort bubble weight." The animals should be sorted by weight in ascending order.The swap count should be 108, and the compare count should be 210.
Zoo."sort insertion weight".The animals should be sorted by weight in ascending order. The swap count should be 108, and the compare count should be 20.
Zoo."sort selection weight." The animals should be sorted by weight in ascending order.The swap count should be 16, and the compare count should be 210.

Penjee via https://blog.penjee.com/binary-vs-linear-search-animated-gifs/
"search" case add an if-statement that tests if the second word passed in is either "binary" or "linear.""linear."InitialUpper and passing commandWords[2] in a local variable.Each time through, increment the loop counter and check if the current animal's name matches the passed-in name.
Then break. The message should look like the following.
[animal name] found. [number of loops] loops complete."binary."InitialUpper and passing commandWords[2] in a local variable.Also, store a copy of the zoo's animals from a SortResult object in a variable, as shown in the code below.
SortResult animals = zoo.SortAnimals("bubble", "name");minPosition variable to 0 and a maxPosition variable to one less than the number of animals in the list.minPosition is less than or equal to the maxPosition.minPosition and maxPosition together (this will need to be in parentheses) and dividing by 2. Store this in an int type named middlePosition.string.Compare to compare the name being searched for with the name of the animal at the middlePosition. Store the result in an int type named compare.minPosition to one more than the middlePosition.maxPosition to one less than the middlePosition.Type "search linear Ted". Ensure that Ted is found and that it takes 16 loops to complete.
"search binary Ted".minPosition, maxPosition, middlePosition and the compare variable.The minPosition should be 0, the maxPosition should be 20, the middlePosition should become 10 and the compare variable should be 1 (indicating it is greater than the animal name at the middle position). The minPosition should become 11 because we now only care about the ranges from 11 to 20.
Now the middle position becomes 15 and the minPosition becomes 16. The range was cut in half again.
minPosition becomes 19.The middle position becomes 19 and since the animal at position 19 IS Ted, the string compare will come back as 0 and the else statement will execute.
Zoo class' SortAnimals method© 2026 Northcentral Technical College