void quicksort(int leftIn, int rightIn, IntComparator compare, RandomSeed seed) { if (leftIn >= rightIn) return; int left = leftIn; int right = rightIn; while (true) // tail recursion loop { if (right - left < 9) { insertionsort(left, right + 1, compare); return; } // Select random index for the pivot seed.random = NumberUtils.nextRand(seed.random); long nom = ((long) (right - left)) * seed.random; int pivotIndex = (int) (nom / NumberUtils.intMax()) + left; // Get the pivot value int pivotValue = m_buffer[pivotIndex]; // Start partition // Move pivot to the right swap(pivotIndex, right); int storeIndex = left; for (int i = left; i < right; i++) { int elm = m_buffer[i]; if (compare.compare(elm, pivotValue) <= 0) { swap(storeIndex, i); storeIndex = storeIndex + 1; } } // Move pivot to its final place swap(storeIndex, right); // End partition // Shorter part is regular recursion // Longer part is tail recursion if (storeIndex - left < right - storeIndex) { quicksort(left, storeIndex - 1, compare, seed); left = storeIndex + 1; } else { quicksort(storeIndex + 1, right, compare, seed); right = storeIndex - 1; } } }