title: Get Array Max and Min between Start and End Indexes
body: <p>I need ideas for a simple, lightweight way to get the <strong>max</strong> and <strong>min</strong> values of an array of doubles. The trick is <em>I only need the max and min between two indexes in the array.</em></p>
  
  <p>The built-in <strong>arrayOfDoubles.Max()</strong> and <strong>arrayOfDoubles.Min()</strong> will <em>not</em> work, because they check the entire array.</p>
  
  <p>This code will be running constantly and needs to be somewhat efficient. That said, <strong>simplicity and readability</strong> are more important than speed alone.</p>
  
  <p>Here's one way to get the max and min between two indexes:</p>
  
  <pre><code>double[] array = new double[8] { 3, 1, 15, 5, 11, 9, 13, 7 };
  
  int startIndex = 3;
  int endIndex = 6;
  
  // Get max and min between these two indexes in the array
  double max = GetMax(array, startIndex, endIndex);
  double min = GetMin(array, startIndex, endIndex);
  
  Console.WriteLine("Max = " + max + ", Min = " + min);
  </code></pre>
  
  <p>Here is the code for <strong>GetMax</strong>, and <strong>GetMin</strong> would be very similar:</p>
  
  <pre><code>private static double GetMax(double[] array, int startIndex, int endIndex)
  {
      double max = double.MinValue;
  
      for (int i = startIndex; i &lt;= endIndex; i++)
      {
          // Increase max with each bigger value
          max = Math.Max(max, array[i]);
      }
  
      return max;
  }
  </code></pre>
  
  <p>And the output: <code>Max = 13, Min = 5</code></p>
  
  <p><strong>The Question:</strong> What other ways could I get the same result that might be simpler and wouldn't require too much overhead?</p>