To speedup, we can define variable "diff" early as a local variable. Further, we can use the following piece of code to replace Math.abs, which takes longer time:
diff = A[i] - A[j]; if (diff < 0) diff = 0 - diff;