title: Efficient algorithm fir finding the smallest and largest elements of an array
body: <p>I'm trying to achieve time complexity O (n log n) for my algorithm that I have been using. What it does is looks at a list of numbers and first the highest number, lowest number, and the profit obtained from the two. How do I modified my code to get the time complexity I am looking for?</p>
  
  <p>Code: </p>
  
  <pre><code>while (i &lt; a.length)
        {
           for (int j = i; j &lt; a.length; j++)
           {
              if(a[j] - a[i] &gt; profit)
              {
                 lowestIndex = i;
                 highestIndex = j;
                 profit = a[j] - a[i];
              }
           }  
           i++;
        }  
  </code></pre>