Class BufferSort


  • public class BufferSort
    extends java.lang.Object
    Sorting algorithms for nio buffers.
    Author:
    biteytech@protonmail.com, heap-sort adapted from programiz.com
    • Constructor Summary

      Constructors 
      Constructor Description
      BufferSort()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void countingSort​(java.nio.ByteBuffer b, int fromIndex, int toIndex)
      Sorts a range of the specified ByteBuffer in ascending order (lowest first).
      static void countingSort​(java.nio.ShortBuffer b, int fromIndex, int toIndex)
      Sorts a range of the specified ShortBuffer in ascending order (lowest first).
      static void heapSort​(java.nio.ByteBuffer b, int fromIndex, int toIndex)
      Sorts a range of the specified ByteBuffer in ascending order (lowest first).
      static void heapSort​(java.nio.DoubleBuffer b, int fromIndex, int toIndex)
      Sorts a range of the specified DoubleBuffer in ascending order (lowest first).
      static void heapSort​(java.nio.FloatBuffer b, int fromIndex, int toIndex)
      Sorts a range of the specified FloatBuffer in ascending order (lowest first).
      static void heapSort​(java.nio.IntBuffer b, int fromIndex, int toIndex)
      Sorts a range of the specified IntBuffer in ascending order (lowest first).
      static void heapSort​(java.nio.IntBuffer b, java.util.function.IntBinaryOperator comparator, int fromIndex, int toIndex)
      Sorts a range of the specified IntBuffer in ascending order (lowest first).
      static void heapSort​(java.nio.LongBuffer b, int fromIndex, int toIndex)
      Sorts a range of the specified LongBuffer in ascending order (lowest first).
      static void heapSort​(java.nio.ShortBuffer b, int fromIndex, int toIndex)
      Sorts a range of the specified ShortBuffer in ascending order (lowest first).
      static void insertionSort​(java.nio.ByteBuffer b, int fromIndex, int toIndex)
      Sorts a range of the specified ByteBuffer in ascending order (lowest first).
      static void insertionSort​(java.nio.DoubleBuffer b, int fromIndex, int toIndex)
      Sorts a range of the specified DoubleBuffer in ascending order (lowest first).
      static void insertionSort​(java.nio.FloatBuffer b, int fromIndex, int toIndex)
      Sorts a range of the specified FloatBuffer in ascending order (lowest first).
      static void insertionSort​(java.nio.IntBuffer b, int fromIndex, int toIndex)
      Sorts a range of the specified IntBuffer in ascending order (lowest first).
      static void insertionSort​(java.nio.LongBuffer b, int fromIndex, int toIndex)
      Sorts a range of the specified LongBuffer in ascending order (lowest first).
      static void insertionSort​(java.nio.ShortBuffer b, int fromIndex, int toIndex)
      Sorts a range of the specified ShortBuffer in ascending order (lowest first).
      static void radixSort​(java.nio.IntBuffer b, int fromIndex, int toIndex)
      Sorts a range of the specified IntBuffer in ascending order (lowest first).
      static void radixSort​(java.nio.LongBuffer b, int fromIndex, int toIndex)
      Sorts a range of the specified LongBuffer in ascending order (lowest first).
      static void sort​(java.nio.ByteBuffer b, int fromIndex, int toIndex)
      Sorts a range of the specified ByteBuffer in ascending order (lowest first).
      static void sort​(java.nio.DoubleBuffer b, int fromIndex, int toIndex)
      Sorts a range of the specified DoubleBuffer in ascending order (lowest first).
      static void sort​(java.nio.FloatBuffer b, int fromIndex, int toIndex)
      Sorts a range of the specified FloatBuffer in ascending order (lowest first).
      static void sort​(java.nio.IntBuffer b, int fromIndex, int toIndex)
      Sorts a range of the specified IntBuffer in ascending order (lowest first).
      static void sort​(java.nio.LongBuffer b, int fromIndex, int toIndex)
      Sorts a range of the specified LongBuffer in ascending order (lowest first).
      static void sort​(java.nio.ShortBuffer b, int fromIndex, int toIndex)
      Sorts a range of the specified ShortBuffer in ascending order (lowest first).
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • BufferSort

        public BufferSort()
    • Method Detail

      • heapSort

        public static void heapSort​(java.nio.IntBuffer b,
                                    int fromIndex,
                                    int toIndex)
        Sorts a range of the specified IntBuffer in ascending order (lowest first). The sort is:
        • in-place
        • O(n*log(n)) in the worst case
        • a good general-purpose sorting algorithm
        Parameters:
        b - the buffer to be sorted
        fromIndex - the index of the first element (inclusive) to be sorted
        toIndex - the index of the last element (exclusive) to be sorted
        Throws:
        java.lang.IllegalArgumentException - if fromIndex > toIndex
        java.lang.IndexOutOfBoundsException - if fromIndex < 0 or toIndex > b.capacity()
      • heapSort

        public static void heapSort​(java.nio.IntBuffer b,
                                    java.util.function.IntBinaryOperator comparator,
                                    int fromIndex,
                                    int toIndex)
        Sorts a range of the specified IntBuffer in ascending order (lowest first). The sort is:
        • in-place
        • O(n*log(n)) in the worst case
        • a good general-purpose sorting algorithm
        Parameters:
        b - the buffer to be sorted
        comparator - used to compare values from b. useful when the integers are identifiers or indices referencing some external data structure.
        fromIndex - the index of the first element (inclusive) to be sorted
        toIndex - the index of the last element (exclusive) to be sorted
        Throws:
        java.lang.IllegalArgumentException - if fromIndex > toIndex
        java.lang.IndexOutOfBoundsException - if fromIndex < 0 or toIndex > b.capacity()
      • heapSort

        public static void heapSort​(java.nio.LongBuffer b,
                                    int fromIndex,
                                    int toIndex)
        Sorts a range of the specified LongBuffer in ascending order (lowest first). The sort is:
        • in-place
        • O(n*log(n)) in the worst case
        • a good general-purpose sorting algorithm
        Parameters:
        b - the buffer to be sorted
        fromIndex - the index of the first element (inclusive) to be sorted
        toIndex - the index of the last element (exclusive) to be sorted
        Throws:
        java.lang.IllegalArgumentException - if fromIndex > toIndex
        java.lang.IndexOutOfBoundsException - if fromIndex < 0 or toIndex > b.capacity()
      • heapSort

        public static void heapSort​(java.nio.ShortBuffer b,
                                    int fromIndex,
                                    int toIndex)
        Sorts a range of the specified ShortBuffer in ascending order (lowest first). The sort is:
        • in-place
        • O(n*log(n)) in the worst case
        • a good general-purpose sorting algorithm
        Parameters:
        b - the buffer to be sorted
        fromIndex - the index of the first element (inclusive) to be sorted
        toIndex - the index of the last element (exclusive) to be sorted
        Throws:
        java.lang.IllegalArgumentException - if fromIndex > toIndex
        java.lang.IndexOutOfBoundsException - if fromIndex < 0 or toIndex > b.capacity()
      • heapSort

        public static void heapSort​(java.nio.ByteBuffer b,
                                    int fromIndex,
                                    int toIndex)
        Sorts a range of the specified ByteBuffer in ascending order (lowest first). The sort is:
        • in-place
        • O(n*log(n)) in the worst case
        • a good general-purpose sorting algorithm
        Parameters:
        b - the buffer to be sorted
        fromIndex - the index of the first element (inclusive) to be sorted
        toIndex - the index of the last element (exclusive) to be sorted
        Throws:
        java.lang.IllegalArgumentException - if fromIndex > toIndex
        java.lang.IndexOutOfBoundsException - if fromIndex < 0 or toIndex > b.capacity()
      • heapSort

        public static void heapSort​(java.nio.FloatBuffer b,
                                    int fromIndex,
                                    int toIndex)
        Sorts a range of the specified FloatBuffer in ascending order (lowest first). The sort is:
        • in-place
        • O(n*log(n)) in the worst case
        • a good general-purpose sorting algorithm
        This method considers all NaN values to be equal to each other and greater than all other values (including Float.POSITIVE_INFINITY).
        Parameters:
        b - the buffer to be sorted
        fromIndex - the index of the first element (inclusive) to be sorted
        toIndex - the index of the last element (exclusive) to be sorted
        Throws:
        java.lang.IllegalArgumentException - if fromIndex > toIndex
        java.lang.IndexOutOfBoundsException - if fromIndex < 0 or toIndex > b.capacity()
      • heapSort

        public static void heapSort​(java.nio.DoubleBuffer b,
                                    int fromIndex,
                                    int toIndex)
        Sorts a range of the specified DoubleBuffer in ascending order (lowest first). The sort is:
        • in-place
        • O(n*log(n)) in the worst case
        • a good general-purpose sorting algorithm
        This method considers all NaN values to be equal to each other and greater than all other values (including Double.POSITIVE_INFINITY).
        Parameters:
        b - the buffer to be sorted
        fromIndex - the index of the first element (inclusive) to be sorted
        toIndex - the index of the last element (exclusive) to be sorted
        Throws:
        java.lang.IllegalArgumentException - if fromIndex > toIndex
        java.lang.IndexOutOfBoundsException - if fromIndex < 0 or toIndex > b.capacity()
      • radixSort

        public static void radixSort​(java.nio.IntBuffer b,
                                     int fromIndex,
                                     int toIndex)
        Sorts a range of the specified IntBuffer in ascending order (lowest first). The sort is:
        • in-place
        • O(n) in the worst case. However, radix sort has more overhead than heat sort, and is only faster for large ranges.
        Parameters:
        b - the buffer to be sorted
        fromIndex - the index of the first element (inclusive) to be sorted
        toIndex - the index of the last element (exclusive) to be sorted
        Throws:
        java.lang.IllegalArgumentException - if fromIndex > toIndex
        java.lang.IndexOutOfBoundsException - if fromIndex < 0 or toIndex > b.capacity()
      • radixSort

        public static void radixSort​(java.nio.LongBuffer b,
                                     int fromIndex,
                                     int toIndex)
        Sorts a range of the specified LongBuffer in ascending order (lowest first). The sort is:
        • in-place
        • O(n) in the worst case. However, radix sort has more overhead than heat sort, and is only faster for large ranges.
        Parameters:
        b - the buffer to be sorted
        fromIndex - the index of the first element (inclusive) to be sorted
        toIndex - the index of the last element (exclusive) to be sorted
        Throws:
        java.lang.IllegalArgumentException - if fromIndex > toIndex
        java.lang.IndexOutOfBoundsException - if fromIndex < 0 or toIndex > b.capacity()
      • countingSort

        public static void countingSort​(java.nio.ShortBuffer b,
                                        int fromIndex,
                                        int toIndex)
        Sorts a range of the specified ShortBuffer in ascending order (lowest first). This sort is O(n) in the worst case, but it creates and iterates over an int array of length 2^16.
        Parameters:
        b - the buffer to be sorted
        fromIndex - the index of the first element (inclusive) to be sorted
        toIndex - the index of the last element (exclusive) to be sorted
        Throws:
        java.lang.IllegalArgumentException - if fromIndex > toIndex
        java.lang.IndexOutOfBoundsException - if fromIndex < 0 or toIndex > b.capacity()
      • countingSort

        public static void countingSort​(java.nio.ByteBuffer b,
                                        int fromIndex,
                                        int toIndex)
        Sorts a range of the specified ByteBuffer in ascending order (lowest first). This sort is O(n) in the worst case, but it creates and iterates over an int array of length 2^8.
        Parameters:
        b - the buffer to be sorted
        fromIndex - the index of the first element (inclusive) to be sorted
        toIndex - the index of the last element (exclusive) to be sorted
        Throws:
        java.lang.IllegalArgumentException - if fromIndex > toIndex
        java.lang.IndexOutOfBoundsException - if fromIndex < 0 or toIndex > b.capacity()
      • insertionSort

        public static void insertionSort​(java.nio.IntBuffer b,
                                         int fromIndex,
                                         int toIndex)
        Sorts a range of the specified IntBuffer in ascending order (lowest first). The sort is:
        • in-place
        • O(n^2) in the worst case. However, insertion sort has less overhead than heat sort, and is faster for small ranges.
        Parameters:
        b - the buffer to be sorted
        fromIndex - the index of the first element (inclusive) to be sorted
        toIndex - the index of the last element (exclusive) to be sorted
        Throws:
        java.lang.IllegalArgumentException - if fromIndex > toIndex
        java.lang.IndexOutOfBoundsException - if fromIndex < 0 or toIndex > b.capacity()
      • insertionSort

        public static void insertionSort​(java.nio.LongBuffer b,
                                         int fromIndex,
                                         int toIndex)
        Sorts a range of the specified LongBuffer in ascending order (lowest first). The sort is:
        • in-place
        • O(n^2) in the worst case. However, insertion sort has less overhead than heat sort, and is faster for small ranges.
        Parameters:
        b - the buffer to be sorted
        fromIndex - the index of the first element (inclusive) to be sorted
        toIndex - the index of the last element (exclusive) to be sorted
        Throws:
        java.lang.IllegalArgumentException - if fromIndex > toIndex
        java.lang.IndexOutOfBoundsException - if fromIndex < 0 or toIndex > b.capacity()
      • insertionSort

        public static void insertionSort​(java.nio.ShortBuffer b,
                                         int fromIndex,
                                         int toIndex)
        Sorts a range of the specified ShortBuffer in ascending order (lowest first). The sort is:
        • in-place
        • O(n^2) in the worst case. However, insertion sort has less overhead than heat sort, and is faster for small ranges.
        Parameters:
        b - the buffer to be sorted
        fromIndex - the index of the first element (inclusive) to be sorted
        toIndex - the index of the last element (exclusive) to be sorted
        Throws:
        java.lang.IllegalArgumentException - if fromIndex > toIndex
        java.lang.IndexOutOfBoundsException - if fromIndex < 0 or toIndex > b.capacity()
      • insertionSort

        public static void insertionSort​(java.nio.ByteBuffer b,
                                         int fromIndex,
                                         int toIndex)
        Sorts a range of the specified ByteBuffer in ascending order (lowest first). The sort is:
        • in-place
        • O(n^2) in the worst case. However, insertion sort has less overhead than heat sort, and is faster for small ranges.
        Parameters:
        b - the buffer to be sorted
        fromIndex - the index of the first element (inclusive) to be sorted
        toIndex - the index of the last element (exclusive) to be sorted
        Throws:
        java.lang.IllegalArgumentException - if fromIndex > toIndex
        java.lang.IndexOutOfBoundsException - if fromIndex < 0 or toIndex > b.capacity()
      • insertionSort

        public static void insertionSort​(java.nio.FloatBuffer b,
                                         int fromIndex,
                                         int toIndex)
        Sorts a range of the specified FloatBuffer in ascending order (lowest first). The sort is:
        • in-place
        • O(n^2) in the worst case. However, insertion sort has less overhead than heat sort, and is faster for small ranges.
        This method considers all NaN values to be equal to each other and greater than all other values (including Float.POSITIVE_INFINITY).
        Parameters:
        b - the buffer to be sorted
        fromIndex - the index of the first element (inclusive) to be sorted
        toIndex - the index of the last element (exclusive) to be sorted
        Throws:
        java.lang.IllegalArgumentException - if fromIndex > toIndex
        java.lang.IndexOutOfBoundsException - if fromIndex < 0 or toIndex > b.capacity()
      • insertionSort

        public static void insertionSort​(java.nio.DoubleBuffer b,
                                         int fromIndex,
                                         int toIndex)
        Sorts a range of the specified DoubleBuffer in ascending order (lowest first). The sort is:
        • in-place
        • O(n^2) in the worst case. However, insertion sort has less overhead than heat sort, and is faster for small ranges.
        This method considers all NaN values to be equal to each other and greater than all other values (including Double.POSITIVE_INFINITY).
        Parameters:
        b - the buffer to be sorted
        fromIndex - the index of the first element (inclusive) to be sorted
        toIndex - the index of the last element (exclusive) to be sorted
        Throws:
        java.lang.IllegalArgumentException - if fromIndex > toIndex
        java.lang.IndexOutOfBoundsException - if fromIndex < 0 or toIndex > b.capacity()
      • sort

        public static void sort​(java.nio.IntBuffer b,
                                int fromIndex,
                                int toIndex)
        Sorts a range of the specified IntBuffer in ascending order (lowest first). The actual sorting algorithm used depends on the length of the range:
        Length Algorithm
        [0 - 100) insertionSort
        [100 - 10^7) heapSort
        10^7+ radixSort
        Parameters:
        b - the buffer to be sorted
        fromIndex - the index of the first element (inclusive) to be sorted
        toIndex - the index of the last element (exclusive) to be sorted
        Throws:
        java.lang.IllegalArgumentException - if fromIndex > toIndex
        java.lang.IndexOutOfBoundsException - if fromIndex < 0 or toIndex > b.capacity()
      • sort

        public static void sort​(java.nio.LongBuffer b,
                                int fromIndex,
                                int toIndex)
        Sorts a range of the specified LongBuffer in ascending order (lowest first). The actual sorting algorithm used depends on the length of the range:
        Length Algorithm
        [0 - 100) insertionSort
        [100 - 10^7) heapSort
        10^7+ radixSort
        Parameters:
        b - the buffer to be sorted
        fromIndex - the index of the first element (inclusive) to be sorted
        toIndex - the index of the last element (exclusive) to be sorted
        Throws:
        java.lang.IllegalArgumentException - if fromIndex > toIndex
        java.lang.IndexOutOfBoundsException - if fromIndex < 0 or toIndex > b.capacity()
      • sort

        public static void sort​(java.nio.ShortBuffer b,
                                int fromIndex,
                                int toIndex)
        Sorts a range of the specified ShortBuffer in ascending order (lowest first). The actual sorting algorithm used depends on the length of the range:
        Length Algorithm
        [0 - 100) insertionSort
        [100 - 10^7) heapSort
        10^7+ countingSort
        Parameters:
        b - the buffer to be sorted
        fromIndex - the index of the first element (inclusive) to be sorted
        toIndex - the index of the last element (exclusive) to be sorted
        Throws:
        java.lang.IllegalArgumentException - if fromIndex > toIndex
        java.lang.IndexOutOfBoundsException - if fromIndex < 0 or toIndex > b.capacity()
      • sort

        public static void sort​(java.nio.ByteBuffer b,
                                int fromIndex,
                                int toIndex)
        Sorts a range of the specified ByteBuffer in ascending order (lowest first). The actual sorting algorithm used depends on the length of the range:
        Length Algorithm
        [0 - 100) insertionSort
        [100 - 10^5) heapSort
        10^5+ countingSort
        Parameters:
        b - the buffer to be sorted
        fromIndex - the index of the first element (inclusive) to be sorted
        toIndex - the index of the last element (exclusive) to be sorted
        Throws:
        java.lang.IllegalArgumentException - if fromIndex > toIndex
        java.lang.IndexOutOfBoundsException - if fromIndex < 0 or toIndex > b.capacity()
      • sort

        public static void sort​(java.nio.FloatBuffer b,
                                int fromIndex,
                                int toIndex)
        Sorts a range of the specified FloatBuffer in ascending order (lowest first). The actual sorting algorithm used depends on the length of the range:
        Length Algorithm
        [0 - 100) insertionSort
        100+ heapSort
        Parameters:
        b - the buffer to be sorted
        fromIndex - the index of the first element (inclusive) to be sorted
        toIndex - the index of the last element (exclusive) to be sorted
        Throws:
        java.lang.IllegalArgumentException - if fromIndex > toIndex
        java.lang.IndexOutOfBoundsException - if fromIndex < 0 or toIndex > b.capacity()
      • sort

        public static void sort​(java.nio.DoubleBuffer b,
                                int fromIndex,
                                int toIndex)
        Sorts a range of the specified DoubleBuffer in ascending order (lowest first). The actual sorting algorithm used depends on the length of the range:
        Length Algorithm
        [0 - 100) insertionSort
        100+ heapSort
        Parameters:
        b - the buffer to be sorted
        fromIndex - the index of the first element (inclusive) to be sorted
        toIndex - the index of the last element (exclusive) to be sorted
        Throws:
        java.lang.IllegalArgumentException - if fromIndex > toIndex
        java.lang.IndexOutOfBoundsException - if fromIndex < 0 or toIndex > b.capacity()