Package com.github.tommyettinger.ds
Class QuickSelect
java.lang.Object
com.github.tommyettinger.ds.QuickSelect
Implementation of Tony Hoare's quickselect algorithm. Running time is generally O(n), but worst case is O(n^2).
Pivot choice is median of three method, providing better performance than a random pivot for partially sorted data.
See Wikipedia's Quickselect article for more.
Everything here is public so that it can be adapted for use in other codebases that may also use a select algorithm. Not everything here is documented, so use at your own risk. Note that the
Everything here is public so that it can be adapted for use in other codebases that may also use a select algorithm. Not everything here is documented, so use at your own risk. Note that the
k and n parameters are
always 1-based; using 0 for k or n is a bad idea.-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> intmedianOfThreePivot(Arrangeable.ArrangeableList<T> items, Comparator<? super T> comp, int leftIdx, int rightIdx) Median of Three has the potential to outperform a random pivot, especially for partially sorted arraysstatic intmedianOfThreePivot(BooleanList items, BooleanComparator comp, int leftIdx, int rightIdx) Median of Three has the potential to outperform a random pivot, especially for partially sorted arraysstatic intmedianOfThreePivot(ByteList items, ByteComparator comp, int leftIdx, int rightIdx) Median of Three has the potential to outperform a random pivot, especially for partially sorted arraysstatic intmedianOfThreePivot(CharList items, CharComparator comp, int leftIdx, int rightIdx) Median of Three has the potential to outperform a random pivot, especially for partially sorted arraysstatic intmedianOfThreePivot(DoubleList items, DoubleComparator comp, int leftIdx, int rightIdx) Median of Three has the potential to outperform a random pivot, especially for partially sorted arraysstatic intmedianOfThreePivot(FloatList items, FloatComparator comp, int leftIdx, int rightIdx) Median of Three has the potential to outperform a random pivot, especially for partially sorted arraysstatic intmedianOfThreePivot(IntList items, IntComparator comp, int leftIdx, int rightIdx) Median of Three has the potential to outperform a random pivot, especially for partially sorted arraysstatic intmedianOfThreePivot(LongList items, LongComparator comp, int leftIdx, int rightIdx) Median of Three has the potential to outperform a random pivot, especially for partially sorted arraysstatic intmedianOfThreePivot(ShortList items, ShortComparator comp, int leftIdx, int rightIdx) Median of Three has the potential to outperform a random pivot, especially for partially sorted arraysstatic <T> intmedianOfThreePivot(T[] items, Comparator<? super T> comp, int leftIdx, int rightIdx) Median of Three has the potential to outperform a random pivot, especially for partially sorted arraysstatic <T> voidmultiSelect(Arrangeable.ArrangeableList<T> items, Comparator<T> comp, int n) Sorts an array so that items come in groups of n unsorted items, with groups sorted between each other.static <T> voidmultiSelect(Arrangeable.ArrangeableList<T> items, Comparator<T> comp, int left, int right, int n) Sorts an array so that items come in groups of n unsorted items, with groups sorted between each other.static voidmultiSelect(BooleanList items, BooleanComparator comp, int n) Sorts an array so that items come in groups of n unsorted items, with groups sorted between each other.static voidmultiSelect(BooleanList items, BooleanComparator comp, int left, int right, int n) Sorts an array so that items come in groups of n unsorted items, with groups sorted between each other.static voidmultiSelect(ByteList items, ByteComparator comp, int n) Sorts an array so that items come in groups of n unsorted items, with groups sorted between each other.static voidmultiSelect(ByteList items, ByteComparator comp, int left, int right, int n) Sorts an array so that items come in groups of n unsorted items, with groups sorted between each other.static voidmultiSelect(CharList items, CharComparator comp, int n) Sorts an array so that items come in groups of n unsorted items, with groups sorted between each other.static voidmultiSelect(CharList items, CharComparator comp, int left, int right, int n) Sorts an array so that items come in groups of n unsorted items, with groups sorted between each other.static voidmultiSelect(DoubleList items, DoubleComparator comp, int n) Sorts an array so that items come in groups of n unsorted items, with groups sorted between each other.static voidmultiSelect(DoubleList items, DoubleComparator comp, int left, int right, int n) Sorts an array so that items come in groups of n unsorted items, with groups sorted between each other.static voidmultiSelect(FloatList items, FloatComparator comp, int n) Sorts an array so that items come in groups of n unsorted items, with groups sorted between each other.static voidmultiSelect(FloatList items, FloatComparator comp, int left, int right, int n) Sorts an array so that items come in groups of n unsorted items, with groups sorted between each other.static voidmultiSelect(IntList items, IntComparator comp, int n) Sorts an array so that items come in groups of n unsorted items, with groups sorted between each other.static voidmultiSelect(IntList items, IntComparator comp, int left, int right, int n) Sorts an array so that items come in groups of n unsorted items, with groups sorted between each other.static voidmultiSelect(LongList items, LongComparator comp, int n) Sorts an array so that items come in groups of n unsorted items, with groups sorted between each other.static voidmultiSelect(LongList items, LongComparator comp, int left, int right, int n) Sorts an array so that items come in groups of n unsorted items, with groups sorted between each other.static voidmultiSelect(ShortList items, ShortComparator comp, int n) Sorts an array so that items come in groups of n unsorted items, with groups sorted between each other.static voidmultiSelect(ShortList items, ShortComparator comp, int left, int right, int n) Sorts an array so that items come in groups of n unsorted items, with groups sorted between each other.static <T> voidmultiSelect(T[] items, Comparator<T> comp, int n) Sorts an array so that items come in groups of n unsorted items, with groups sorted between each other.static <T> voidmultiSelect(T[] items, Comparator<T> comp, int left, int right, int n) Sorts an array so that items come in groups of n unsorted items, with groups sorted between each other.static <T> intpartition(Arrangeable.ArrangeableList<T> items, Comparator<? super T> comp, int left, int right, int pivot) static intpartition(BooleanList items, BooleanComparator comp, int left, int right, int pivot) static intpartition(ByteList items, ByteComparator comp, int left, int right, int pivot) static intpartition(CharList items, CharComparator comp, int left, int right, int pivot) static intpartition(DoubleList items, DoubleComparator comp, int left, int right, int pivot) static intpartition(FloatList items, FloatComparator comp, int left, int right, int pivot) static intpartition(IntList items, IntComparator comp, int left, int right, int pivot) static intpartition(LongList items, LongComparator comp, int left, int right, int pivot) static intpartition(ShortList items, ShortComparator comp, int left, int right, int pivot) static <T> intpartition(T[] items, Comparator<? super T> comp, int left, int right, int pivot) static <T> intrecursiveSelect(Arrangeable.ArrangeableList<T> items, Comparator<? super T> comp, int left, int right, int k) static intrecursiveSelect(BooleanList items, BooleanComparator comp, int left, int right, int k) static intrecursiveSelect(ByteList items, ByteComparator comp, int left, int right, int k) static intrecursiveSelect(CharList items, CharComparator comp, int left, int right, int k) static intrecursiveSelect(DoubleList items, DoubleComparator comp, int left, int right, int k) static intrecursiveSelect(FloatList items, FloatComparator comp, int left, int right, int k) static intrecursiveSelect(IntList items, IntComparator comp, int left, int right, int k) static intrecursiveSelect(LongList items, LongComparator comp, int left, int right, int k) static intrecursiveSelect(ShortList items, ShortComparator comp, int left, int right, int k) static <T> intrecursiveSelect(T[] items, Comparator<? super T> comp, int left, int right, int k) static <T> intselect(Arrangeable.ArrangeableList<T> items, Comparator<? super T> comp, int n, int size) static intselect(BooleanList items, BooleanComparator comp, int n, int size) static intselect(ByteList items, ByteComparator comp, int n, int size) static intselect(CharList items, CharComparator comp, int n, int size) static intselect(DoubleList items, DoubleComparator comp, int n, int size) static intselect(FloatList items, FloatComparator comp, int n, int size) static intselect(IntList items, IntComparator comp, int n, int size) static intselect(LongList items, LongComparator comp, int n, int size) static intselect(ShortList items, ShortComparator comp, int n, int size) static <T> intselect(T[] items, Comparator<? super T> comp, int n, int size) static <T> voidswap(T[] items, int left, int right)
-
Method Details
-
select
-
partition
public static <T> int partition(T[] items, Comparator<? super T> comp, int left, int right, int pivot) -
recursiveSelect
public static <T> int recursiveSelect(T[] items, Comparator<? super T> comp, int left, int right, int k) -
medianOfThreePivot
public static <T> int medianOfThreePivot(T[] items, Comparator<? super T> comp, int leftIdx, int rightIdx) Median of Three has the potential to outperform a random pivot, especially for partially sorted arrays -
swap
public static <T> void swap(T[] items, int left, int right) -
multiSelect
Sorts an array so that items come in groups of n unsorted items, with groups sorted between each other. This combines a selection algorithm with a binary divide and conquer approach.- Type Parameters:
T- the type of elements of items- Parameters:
items- the T elements to be partially sortedcomp- a Comparator for the T elementsn- the size of the partially-sorted sections to produce
-
multiSelect
Sorts an array so that items come in groups of n unsorted items, with groups sorted between each other. This combines a selection algorithm with a binary divide and conquer approach.- Type Parameters:
T- the type of elements of items- Parameters:
items- the T elements to be partially sortedcomp- a Comparator for the T elementsleft- the lower index (inclusive)right- the upper index (inclusive)n- the size of the partially-sorted sections to produce
-
select
public static <T> int select(Arrangeable.ArrangeableList<T> items, Comparator<? super T> comp, int n, int size) -
partition
public static <T> int partition(Arrangeable.ArrangeableList<T> items, Comparator<? super T> comp, int left, int right, int pivot) -
recursiveSelect
public static <T> int recursiveSelect(Arrangeable.ArrangeableList<T> items, Comparator<? super T> comp, int left, int right, int k) -
medianOfThreePivot
public static <T> int medianOfThreePivot(Arrangeable.ArrangeableList<T> items, Comparator<? super T> comp, int leftIdx, int rightIdx) Median of Three has the potential to outperform a random pivot, especially for partially sorted arrays -
multiSelect
Sorts an array so that items come in groups of n unsorted items, with groups sorted between each other. This combines a selection algorithm with a binary divide and conquer approach.- Type Parameters:
T- the type of elements of items- Parameters:
items- the T elements to be partially sortedcomp- a Comparator for the T elementsn- the size of the partially-sorted sections to produce
-
multiSelect
public static <T> void multiSelect(Arrangeable.ArrangeableList<T> items, Comparator<T> comp, int left, int right, int n) Sorts an array so that items come in groups of n unsorted items, with groups sorted between each other. This combines a selection algorithm with a binary divide and conquer approach.- Type Parameters:
T- the type of elements of items- Parameters:
items- the T elements to be partially sortedcomp- a Comparator for the T elementsleft- the lower index (inclusive)right- the upper index (inclusive)n- the size of the partially-sorted sections to produce
-
select
-
partition
-
recursiveSelect
-
medianOfThreePivot
Median of Three has the potential to outperform a random pivot, especially for partially sorted arrays -
multiSelect
Sorts an array so that items come in groups of n unsorted items, with groups sorted between each other. This combines a selection algorithm with a binary divide and conquer approach.- Parameters:
items- the int elements to be partially sortedcomp- an IntComparator for the int elementsn- the size of the partially-sorted sections to produce
-
multiSelect
Sorts an array so that items come in groups of n unsorted items, with groups sorted between each other. This combines a selection algorithm with a binary divide and conquer approach.- Parameters:
items- the int elements to be partially sortedcomp- an IntComparator for the int elementsleft- the lower index (inclusive)right- the upper index (inclusive)n- the size of the partially-sorted sections to produce
-
select
-
partition
-
recursiveSelect
-
medianOfThreePivot
public static int medianOfThreePivot(LongList items, LongComparator comp, int leftIdx, int rightIdx) Median of Three has the potential to outperform a random pivot, especially for partially sorted arrays -
multiSelect
Sorts an array so that items come in groups of n unsorted items, with groups sorted between each other. This combines a selection algorithm with a binary divide and conquer approach.- Parameters:
items- the long elements to be partially sortedcomp- a LongComparator for the long elementsn- the size of the partially-sorted sections to produce
-
multiSelect
Sorts an array so that items come in groups of n unsorted items, with groups sorted between each other. This combines a selection algorithm with a binary divide and conquer approach.- Parameters:
items- the long elements to be partially sortedcomp- a LongComparator for the long elementsleft- the lower index (inclusive)right- the upper index (inclusive)n- the size of the partially-sorted sections to produce
-
select
-
partition
-
recursiveSelect
public static int recursiveSelect(FloatList items, FloatComparator comp, int left, int right, int k) -
medianOfThreePivot
public static int medianOfThreePivot(FloatList items, FloatComparator comp, int leftIdx, int rightIdx) Median of Three has the potential to outperform a random pivot, especially for partially sorted arrays -
multiSelect
Sorts an array so that items come in groups of n unsorted items, with groups sorted between each other. This combines a selection algorithm with a binary divide and conquer approach.- Parameters:
items- the float elements to be partially sortedcomp- a FloatComparator for the long elementsn- the size of the partially-sorted sections to produce
-
multiSelect
Sorts an array so that items come in groups of n unsorted items, with groups sorted between each other. This combines a selection algorithm with a binary divide and conquer approach.- Parameters:
items- the float elements to be partially sortedcomp- a FloatComparator for the long elementsleft- the lower index (inclusive)right- the upper index (inclusive)n- the size of the partially-sorted sections to produce
-
select
-
partition
public static int partition(DoubleList items, DoubleComparator comp, int left, int right, int pivot) -
recursiveSelect
public static int recursiveSelect(DoubleList items, DoubleComparator comp, int left, int right, int k) -
medianOfThreePivot
public static int medianOfThreePivot(DoubleList items, DoubleComparator comp, int leftIdx, int rightIdx) Median of Three has the potential to outperform a random pivot, especially for partially sorted arrays -
multiSelect
Sorts an array so that items come in groups of n unsorted items, with groups sorted between each other. This combines a selection algorithm with a binary divide and conquer approach.- Parameters:
items- the double elements to be partially sortedcomp- a DoubleComparator for the double elementsn- the size of the partially-sorted sections to produce
-
multiSelect
Sorts an array so that items come in groups of n unsorted items, with groups sorted between each other. This combines a selection algorithm with a binary divide and conquer approach.- Parameters:
items- the double elements to be partially sortedcomp- a DoubleComparator for the double elementsleft- the lower index (inclusive)right- the upper index (inclusive)n- the size of the partially-sorted sections to produce
-
select
-
partition
-
recursiveSelect
public static int recursiveSelect(ShortList items, ShortComparator comp, int left, int right, int k) -
medianOfThreePivot
public static int medianOfThreePivot(ShortList items, ShortComparator comp, int leftIdx, int rightIdx) Median of Three has the potential to outperform a random pivot, especially for partially sorted arrays -
multiSelect
Sorts an array so that items come in groups of n unsorted items, with groups sorted between each other. This combines a selection algorithm with a binary divide and conquer approach.- Parameters:
items- the short elements to be partially sortedcomp- a ShortComparator for the short elementsn- the size of the partially-sorted sections to produce
-
multiSelect
Sorts an array so that items come in groups of n unsorted items, with groups sorted between each other. This combines a selection algorithm with a binary divide and conquer approach.- Parameters:
items- the short elements to be partially sortedcomp- a ShortComparator for the short elementsleft- the lower index (inclusive)right- the upper index (inclusive)n- the size of the partially-sorted sections to produce
-
select
-
partition
-
recursiveSelect
-
medianOfThreePivot
public static int medianOfThreePivot(ByteList items, ByteComparator comp, int leftIdx, int rightIdx) Median of Three has the potential to outperform a random pivot, especially for partially sorted arrays -
multiSelect
Sorts an array so that items come in groups of n unsorted items, with groups sorted between each other. This combines a selection algorithm with a binary divide and conquer approach.- Parameters:
items- the byte elements to be partially sortedcomp- a ByteComparator for the byte elementsn- the size of the partially-sorted sections to produce
-
multiSelect
Sorts an array so that items come in groups of n unsorted items, with groups sorted between each other. This combines a selection algorithm with a binary divide and conquer approach.- Parameters:
items- the byte elements to be partially sortedcomp- a ByteComparator for the byte elementsleft- the lower index (inclusive)right- the upper index (inclusive)n- the size of the partially-sorted sections to produce
-
select
-
partition
-
recursiveSelect
-
medianOfThreePivot
public static int medianOfThreePivot(CharList items, CharComparator comp, int leftIdx, int rightIdx) Median of Three has the potential to outperform a random pivot, especially for partially sorted arrays -
multiSelect
Sorts an array so that items come in groups of n unsorted items, with groups sorted between each other. This combines a selection algorithm with a binary divide and conquer approach.- Parameters:
items- the char elements to be partially sortedcomp- a CharComparator for the char elementsn- the size of the partially-sorted sections to produce
-
multiSelect
Sorts an array so that items come in groups of n unsorted items, with groups sorted between each other. This combines a selection algorithm with a binary divide and conquer approach.- Parameters:
items- the char elements to be partially sortedcomp- a CharComparator for the char elementsleft- the lower index (inclusive)right- the upper index (inclusive)n- the size of the partially-sorted sections to produce
-
select
-
partition
public static int partition(BooleanList items, BooleanComparator comp, int left, int right, int pivot) -
recursiveSelect
public static int recursiveSelect(BooleanList items, BooleanComparator comp, int left, int right, int k) -
medianOfThreePivot
public static int medianOfThreePivot(BooleanList items, BooleanComparator comp, int leftIdx, int rightIdx) Median of Three has the potential to outperform a random pivot, especially for partially sorted arrays -
multiSelect
Sorts an array so that items come in groups of n unsorted items, with groups sorted between each other. This combines a selection algorithm with a binary divide and conquer approach.- Parameters:
items- the boolean elements to be partially sortedcomp- a BooleanComparator for the boolean elementsn- the size of the partially-sorted sections to produce
-
multiSelect
public static void multiSelect(BooleanList items, BooleanComparator comp, int left, int right, int n) Sorts an array so that items come in groups of n unsorted items, with groups sorted between each other. This combines a selection algorithm with a binary divide and conquer approach.- Parameters:
items- the boolean elements to be partially sortedcomp- a BooleanComparator for the boolean elementsleft- the lower index (inclusive)right- the upper index (inclusive)n- the size of the partially-sorted sections to produce
-