title: Find the minimum difference between numbers in a sorted array in bash
body: <p>I am doing a code challenge, with given numbers, I have to find the minimum difference. For exemple : </p>
  
  <pre><code>[3,5,8,9]
  
  Result : 1 (9-8)
  </code></pre>
  
  <p>The problem is that the final test to achieve the puzzle use a very large amount of numbers and my code is not optimised enough.</p>
  
  <p>Before finding the minimu difference, I sort the array like that : </p>
  
  <pre><code>IFS=$'\n' sorted=($(sort -n &lt;&lt;&lt;"${array[*]}"))
  </code></pre>
  
  <p>Then, I do a for loop over the array to find the smallest but it takes too much time so I tried to do <code>i+4</code> instead of <code>i++</code> but I don't think that this is the real problem.</p>
  
  <p>Here is my code to find the smallest : </p>
  
  <pre><code>smallest=5000
  for (( i=2; i&lt;N; i=$((i+1)) ));do
      diff=$((${sorted[i]}-${sorted[i-1]}))
      if [ $diff -lt $smallest ]; then
          smallest=$diff
      fi
  done
  </code></pre>
  
  <p>Do you have any idea of what I can do to have something optimzed enough to go through the test ? By the way, I know almost nothing about Bash, I usaly code in python.</p>