internal void Quicksort(int leftIn, int rightIn, com.epl.geometry.AttributeStreamOfInt32.IntComparator compare, com.epl.geometry.AttributeStreamOfInt32.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 = com.epl.geometry.NumberUtils.NextRand(seed.random); long nom = ((long)(right - left)) * seed.random; int pivotIndex = (int)(nom / com.epl.geometry.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; } } }
internal void Insertionsort(int start, int end, com.epl.geometry.AttributeStreamOfInt32.IntComparator compare) { for (int j = start; j < end; j++) { // insertion sort int key = m_buffer[j]; int i = j - 1; while (i >= start && compare.Compare(m_buffer[i], key) > 0) { m_buffer[i + 1] = m_buffer[i]; i--; } m_buffer[i + 1] = key; } }