Reducing the number of comparisons to find min/max of an array

Viewed 23

I was looking into something simple, as finding the max and min elements of an array (no specific language). I know there are built-in functions in many languages, and also you can write your own, such as:

  1. Initialize max = min = firstElement in Array
  2. Loop through each element, and check if it's less than min or more than max
  3. Update accordingly
  4. Return

This of course, results in k comparisons for an array of size k. Is there a way to reduce the number of comparisons that we would have to do? Just assuming the array is unsorted. I've tagged it in Python as the basic algorithm I coded was in Python.

0 Answers
Related