private void sortTrianglesInternal(int i, int j) {
    int pivotIndex = (i+j)/2;
    sortTrianglesSwap(pivotIndex, j);
    int k = sortTrianglesPartition(i-1, j);
    sortTrianglesSwap(k, j);
    if ((k-i) > 1) sortTrianglesInternal(i, k-1);
    if ((j-k) > 1) sortTrianglesInternal(k+1, j);
  }