org.apache.lucene.util
Class SorterTemplate

java.lang.Object
  extended by org.apache.lucene.util.SorterTemplate

public abstract class SorterTemplate
extends Object

This class was inspired by CGLIB, but provides a better QuickSort algorithm without additional InsertionSort at the end. To use, subclass and override the four abstract methods which compare and modify your data. Allows custom swap so that two arrays can be sorted at the same time.

NOTE: This API is for internal purposes only and might change in incompatible ways in the next release.

Constructor Summary
SorterTemplate()
           
 
Method Summary
 void binarySort(int lo, int hi)
          Sorts via stable in-place BinarySort algorithm (O(n2)) (ideal for small collections which are in random order).
protected abstract  int compare(int i, int j)
          Compares slots i and j of you data.
protected abstract  int comparePivot(int j)
          Implements the compare function for the previously stored pivot value.
 void insertionSort(int lo, int hi)
          Sorts via stable in-place InsertionSort algorithm (O(n2)) (ideal for small collections which are mostly presorted).
protected  void merge(int lo, int pivot, int hi, int len1, int len2)
          Merge the slices [lo-pivot[ (of length len1) and [pivot-hi[ (of length len2) which are already sorted.
 void mergeSort(int lo, int hi)
          Sorts via stable in-place MergeSort algorithm For small collections falls back to insertionSort(int,int).
 void quickSort(int lo, int hi)
          Sorts via in-place, but unstable, QuickSort algorithm.
protected abstract  void setPivot(int i)
          Implement this method, that stores the value of slot i as pivot value
protected abstract  void swap(int i, int j)
          Implement this method, that swaps slots i and j in your data
 void timSort(int lo, int hi)
          Sorts using TimSort, see also source code.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SorterTemplate

public SorterTemplate()
Method Detail

swap

protected abstract void swap(int i,
                             int j)
Implement this method, that swaps slots i and j in your data


compare

protected abstract int compare(int i,
                               int j)
Compares slots i and j of you data. Should be implemented like valueOf(i).compareTo(valueOf(j))


setPivot

protected abstract void setPivot(int i)
Implement this method, that stores the value of slot i as pivot value


comparePivot

protected abstract int comparePivot(int j)
Implements the compare function for the previously stored pivot value. Should be implemented like pivot.compareTo(valueOf(j))


insertionSort

public final void insertionSort(int lo,
                                int hi)
Sorts via stable in-place InsertionSort algorithm (O(n2)) (ideal for small collections which are mostly presorted).


binarySort

public final void binarySort(int lo,
                             int hi)
Sorts via stable in-place BinarySort algorithm (O(n2)) (ideal for small collections which are in random order).


quickSort

public final void quickSort(int lo,
                            int hi)
Sorts via in-place, but unstable, QuickSort algorithm. For small collections falls back to insertionSort(int,int).


timSort

public final void timSort(int lo,
                          int hi)
Sorts using TimSort, see also source code. TimSort is a stable sorting algorithm based on MergeSort but known to perform extremely well on partially-sorted inputs. For small collections, falls back to binarySort(int, int).


mergeSort

public final void mergeSort(int lo,
                            int hi)
Sorts via stable in-place MergeSort algorithm For small collections falls back to insertionSort(int,int).


merge

protected void merge(int lo,
                     int pivot,
                     int hi,
                     int len1,
                     int len2)
Merge the slices [lo-pivot[ (of length len1) and [pivot-hi[ (of length len2) which are already sorted. This method merges in-place but can be extended to provide a faster implementation using extra memory.



Copyright © 2000-2013 Apache Software Foundation. All Rights Reserved.