Problem: A Generic max

You have sorted a whole DynamicArray with selectionSort. Often you do not want the whole array in order, though. You just want the single largest element, like the highest card in a hand or the top score on a board. Let’s write max.

Like swap, indexOfMin, and selectionSort, max goes in ArrayUtils as a static method, and reaches the array only through its public size and get. Note to keep the ArrayUtils class intact and aligned with the chapter notes, we will use a new class MoreArrayUtils for this practice problem.

You will write max in two forms. They are the same two forms we used for selectionSort. The first form is by natural order, for elements that define their own ordering with Comparable. The second form takes a Comparator, for when the order comes from outside the element.

A working solution is provided in the accompanying code for this practice problem, in the practice package (MoreArrayUtils.java). Run scripts/run.sh practice.MaxDemo to run it.

Steps

  1. Solution: Max by Natural Order
  2. Solution: Max by a Comparator