Brooks said:
I agree. The first question I had is how long is the
list. If it is 20 integers then walk through it
comparing each number. If we are talking tens of
thousands then start considering the sorting,
multi-threading, fancy methods.
If you start using parallel processing to get the max of
10 numbers, I would call you crazy.
I think the parallel processing proposal, at least in the
extreme form described, was a joke. Even if I had a VERY
long array, and a multiprocessor, I would chop the linear
scan into one chunk per hardware thread (e.g. 8 chunks if
I were using four dual-threaded processors), spawn a thread
for each chunk, and when they finish linear scan their
results.
Just when would sorting beat linear scan?
If anything, greater length increases the advantage for
linear scan. For short arrays, the lg(n) term in the O(n
lg(n)) complexity of Arrays.sort(int []) is very small, and
could be dominated by differences in how much bookkeeping
each method does. I don't expect anything based on quicksort
to beat linear scan even under those conditions.
When you get up to tens of thousands of elements, the log
base two of the length is in the 13 to 16 range, and
Arrays.sort could lose even if did less, rather than more,
work per comparison than linear scan.
For very long arrays, cache misses become an issue and
linear scan has an additional advantage because it is, well,
linear. There will be at most one cache miss per line and
those misses are easy to predict.
Even a radix sort, which could have O(n) time for int data,
would have a much higher constant multiplier than linear
scan, and be slower in practice.
The only case I've been able to think of so far in which one
should not do linear scan is if operations such as min, max,
and scanning in size order occurred often enough that it was
worth maintaining a TreeSet.
In my opinion, finding the maximum element in an unordered
array or list is one of the rare and delightful cases in
which the simple, easy, direct algorithm is highly
efficient, so there is no messy trade-off between simplicity
and performance, no need to ask "How many elements?".
Patricia