title: Finding the distance between the two closest elements in an array of numbers
body: <p>So I'm teaching myself algorithms from this book I purchased, and I have a pseudo-code for Finding the distance between the two closetst elements in an array of numbers</p>
  
  <pre><code>MinDistance(a[0...n-1])
  Input: Array A of numbers
  Output: Minimum Distance between two of its elements
  dMin &lt;- maximum integer
  
  for i=0 to n-1 do
     for j=0 to n-1 do
        if i!=j and | A[i] - A[j] | &lt; dMin
          dMin = | A[i]-A[j] |
  return dMin
  </code></pre>
  
  <p>However, I wanted to make improvements to this algorithmic solution. Change what's already there, or rewrite all together. Can someone help?
  I wrote the function and class in Java to test the pseudo-code? Is that correct? And once again, how can I make it better from efficiency standpoint. </p>
  
  <pre><code>//Scanner library allowing the user to input data
  import java.lang.Math.*;
  
  public class ArrayTester{
      //algorithm for finding the distance between the two closest elements in an array of numbers
      public int MinDistance(int [] ar){
      int [] a = ar;
      int aSize = a.length;
      int dMin = 0;//MaxInt
      for(int i=0; i&lt; aSize; i++)
      {
          for(int j=i+1; j&lt; aSize;j++)
          {   
              dMin = Math.min(dMin, Math.abs( a[i]-a[j] );
          }
      }
      return dMin;
  }
  
      //MAIN
      public static void main(String[] args){
  
          ArrayTester at = new ArrayTester();
          int [] someArray = {9,1,2,3,16};
          System.out.println("NOT-OPTIMIZED METHOD");
          System.out.println("Array length = "+ someArray.length);
          System.out.println("The distance between the two closest elements: " + at.MinDistance(someArray));
  
      } //end MAIN
  
  } //END CLASS
  </code></pre>
  
  <p>SO I updated the function to minimize calling the Math.abs twice. What else can I do improve it. If I was to rewrite it with sort, would it change my for loops at all, or would it be the same just theoretically run faster.</p>
  
  <pre><code>public int MinDistance(int [] ar){
          int [] a = ar;
          int aSize = a.length;
          int dMin = 0;//MaxInt
          for(int i=0; i&lt; aSize; i++)
          {
              for(int j=i+1; j&lt; aSize;j++)
              {   
                  dMin = Math.min(dMin, Math.abs( a[i]-a[j] );
              }
          }
          return dMin;
      }
  </code></pre>