When finding the largest or smallest element of a list, do we always have to check every value?


#1

Question

In the context of this code challenge, when finding the largest or smallest element of a list, do we always have to check every value?

Answer

This will depend on whether the list is sorted or unsorted.

If the list is unsorted, then yes, you would have to go through every value of the list. This is because we can never be sure if the unchecked values might be the largest or smallest value of the list, as they can appear in any order. So to be certain, we must have checked every single value of the list.

If the list is sorted, then no, we do not have to check every value. We would actually only need to check the first and last elements of the list. This works for lists sorted in ascending or descending order, and would always give us the smallest and largest elements of the list.