Package org.apache.lucene.util
Class IntroSorter
- java.lang.Object
-
- org.apache.lucene.util.Sorter
-
- org.apache.lucene.util.IntroSorter
-
- Direct Known Subclasses:
ArrayIntroSorter,CollectionUtil.ListIntroSorter
public abstract class IntroSorter extends Sorter
Sorterimplementation based on a variant of the quicksort algorithm called introsort: when the recursion level exceeds the log of the length of the array to sort, it falls back to heapsort. This prevents quicksort from running into its worst-case quadratic runtime. Selects the pivot using Tukey's ninther median-of-medians, and partitions using Bentley-McIlroy 3-way partitioning. Small ranges are sorted with insertion sort.This algorithm is NOT stable. It's fast on most data shapes, especially with low cardinality. If the data to sort is known to be strictly ascending or descending, prefer
TimSorter.
-
-
Field Summary
Fields Modifier and Type Field Description (package private) static intSINGLE_MEDIAN_THRESHOLDBelow this size threshold, the partition selection is simplified to a single median.-
Fields inherited from class org.apache.lucene.util.Sorter
BINARY_SORT_THRESHOLD, INSERTION_SORT_THRESHOLD
-
-
Constructor Summary
Constructors Constructor Description IntroSorter()Create a newIntroSorter.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected intcompare(int i, int j)Compare entries found in slotsiandj.protected abstract intcomparePivot(int j)Compare the pivot with the slot atj, similarly tocompare(i, j).private intmedian(int i, int j, int k)Returns the index of the median element among three elements at provided indices.protected abstract voidsetPivot(int i)Save the value at slotiso that it can later be used as a pivot, seeSorter.comparePivot(int).voidsort(int from, int to)Sort the slice which starts atfrom(inclusive) and ends atto(exclusive).(package private) voidsort(int from, int to, int maxDepth)Sorts between from (inclusive) and to (exclusive) with intro sort.-
Methods inherited from class org.apache.lucene.util.Sorter
binarySort, binarySort, checkRange, doRotate, heapChild, heapify, heapParent, heapSort, insertionSort, lower, lower2, mergeInPlace, reverse, rotate, siftDown, swap, upper, upper2
-
-
-
-
Field Detail
-
SINGLE_MEDIAN_THRESHOLD
static final int SINGLE_MEDIAN_THRESHOLD
Below this size threshold, the partition selection is simplified to a single median.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
IntroSorter
public IntroSorter()
Create a newIntroSorter.
-
-
Method Detail
-
sort
public final void sort(int from, int to)Description copied from class:SorterSort the slice which starts atfrom(inclusive) and ends atto(exclusive).
-
sort
void sort(int from, int to, int maxDepth)Sorts between from (inclusive) and to (exclusive) with intro sort.Sorts small ranges with insertion sort. Fallbacks to heap sort to avoid quadratic worst case. Selects the pivot with medians and partitions with the Bentley-McIlroy fast 3-ways algorithm (Engineering a Sort Function, Bentley-McIlroy).
-
median
private int median(int i, int j, int k)Returns the index of the median element among three elements at provided indices.
-
setPivot
protected abstract void setPivot(int i)
Description copied from class:SorterSave the value at slotiso that it can later be used as a pivot, seeSorter.comparePivot(int).
-
comparePivot
protected abstract int comparePivot(int j)
Description copied from class:SorterCompare the pivot with the slot atj, similarly tocompare(i, j).- Overrides:
comparePivotin classSorter
-
-