title: Very fast way to sort array of integers?
body: <p>I come here with a solution I just wrote but I definitely can't find a better way to sort my array as fast as possible.</p>
  
  <p>I actually need the algorithm to give me the answer in less than 1 sec, on an array containing 100'000 integers.</p>
  
  <p>Here's the code :</p>
  
  <pre><code>read N
  for (( i=0; i&lt;N; i++ )); do
      read arr[i]
  done
  
  sorted=($(printf '%s\n' "${arr[@]}"|sort -n))
  
  min=$(( sorted[1]-sorted[0] ))
  
  for (( i=1; i&lt;N-1; i++ )); do
      diff=$(( sorted[i+1] - sorted[i] ))
      if [ "$diff" -lt "$min" ]; then
          min=$diff
      fi
  done
  
  echo $min
  </code></pre>
  
  <p>N is the number of elements, in our case 100'000 as I said.</p>
  
  <p>The thing is, after my array is sorted, I need to loop through it and calculate the minimum distance between two integers :</p>
  
  <p>For example, with (3, 5, 8, 9), the minimum distance is 1 (between 8 and 9).</p>
  
  <p>I'm such a newbie in Bash, so I don't even know if this is an efficient way;</p>
  
  <p>In fact, the gain of speed could come from the second part of the code, and not from the sorting part...</p>
  
  <p>Any ideas ?</p>
  
  <p>Thanks in advance,</p>