private int sortTrianglesPartition(int left, int right) {
    int pivot = right;
    do {
      while (sortTrianglesCompare(++left, pivot) < 0) { }
      while ((right != 0) &&
             (sortTrianglesCompare(--right, pivot) > 0)) { }
      sortTrianglesSwap(left, right);
    } while (left < right);
    sortTrianglesSwap(left, right);
    return left;
  }