Top Description Fields Constructors Methods
com.carrotsearch.hppc.sorting

public final Class QuickSort

extends Object
Class Inheritance
Imports
java.util.function.IntBinaryOperator

In-place Quick sort with 3-way partitioning and ending with Insertion sort.

The sorting is not stable. Performance is O(n.log(n)) and memory is O(1) (although recursion memory is O(log(n))).

Field Summary

Modifier and TypeField and Description
pack-priv static final int
INSERTION_SORT_THRESHOLD

Below this size threshold, the sub-range is sorted using Insertion sort.

pack-priv static final int
SINGLE_MEDIAN_THRESHOLD

/** Below this size threshold, the partition selection is simplified to a single median.

Constructor Summary

AccessConstructor and Description
private
QuickSort()

No instantiation.

Method Summary

Modifier and TypeMethod and Description
private static int
compare(int i, int j, IntBinaryOperator comparator)

Compares two elements at provided indices.

private static void
insertionSort(int fromIndex, int toIndex, IntBinaryOperator comparator, IntBinaryOperator swapper)

Sorts between from (inclusive) and to (exclusive) with insertion sort.

private static int
median(int i, int j, int k, IntBinaryOperator comparator)

Returns the index of the median element among three elements at provided indices.

public static void
sort(int[] array, IntBinaryOperator comparator)

public static void
sort(int[] array, int fromIndex, int toIndex, IntBinaryOperator comparator)

public static void
sort(int
Index where to start sorting in the array, inclusive.
fromIndex
,
int
Index where to stop sorting in the array, exclusive.
toIndex
,
IntBinaryOperator
Compares elements based on their indices. Given indices i and j in the provided array, this comparator returns respectively -1/0/1 if the element at index i is respectively less/equal/greater than the element at index j.
comparator
,
IntBinaryOperator
Swaps the elements in the array at the given indices. For example, a custom swapper may allow sorting two arrays simultaneously.
swapper
)

Performs a recursive in-place Quick sort.

private static void
swap(int i, int j, IntBinaryOperator swapper)

Swaps two elements at provided indices.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

INSERTION_SORT_THRESHOLDback to summary
pack-priv static final int INSERTION_SORT_THRESHOLD

Below this size threshold, the sub-range is sorted using Insertion sort.

SINGLE_MEDIAN_THRESHOLDback to summary
pack-priv static final int SINGLE_MEDIAN_THRESHOLD

/** Below this size threshold, the partition selection is simplified to a single median.

Constructor Detail

QuickSortback to summary
private QuickSort()

No instantiation.

Method Detail

compareback to summary
private static int compare(int i, int j, IntBinaryOperator comparator)

Compares two elements at provided indices.

insertionSortback to summary
private static void insertionSort(int fromIndex, int toIndex, IntBinaryOperator comparator, IntBinaryOperator swapper)

Sorts between from (inclusive) and to (exclusive) with insertion sort.

medianback to summary
private static int median(int i, int j, int k, IntBinaryOperator comparator)

Returns the index of the median element among three elements at provided indices.

sortback to summary
public static void sort(int[] array, IntBinaryOperator comparator)
See Also
sort(int, int, IntBinaryOperator, IntBinaryOperator)
sortback to summary
public static void sort(int[] array, int fromIndex, int toIndex, IntBinaryOperator comparator)
See Also
sort(int, int, IntBinaryOperator, IntBinaryOperator)
sortback to summary
public static void sort(int fromIndex, int toIndex, IntBinaryOperator comparator, IntBinaryOperator swapper)

Performs a recursive in-place Quick sort. The sorting is not stable.

Parameters
fromIndex:int

Index where to start sorting in the array, inclusive.

toIndex:int

Index where to stop sorting in the array, exclusive.

comparator:IntBinaryOperator

Compares elements based on their indices. Given indices i and j in the provided array, this comparator returns respectively -1/0/1 if the element at index i is respectively less/equal/greater than the element at index j.

swapper:IntBinaryOperator

Swaps the elements in the array at the given indices. For example, a custom swapper may allow sorting two arrays simultaneously.

swapback to summary
private static void swap(int i, int j, IntBinaryOperator swapper)

Swaps two elements at provided indices.