Class Sort


  • public class Sort
    extends java.lang.Object
    Efficient sort implementations. Note: These sort implementations were created to compare different sorting algorithms in JavaScript. Don't use them if you don't know what you are doing. Native Array.sort is almost always a better choice.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> void insertionSort​(T[] arr, java.util.Comparator<T> compare)
      Public insertion sort implementation for sorting the entire array.
      static <T> void quicksort​(T[] arr, java.util.Comparator<T> compare)
      This algorithm beats Array.prototype.sort in Chrome only with arrays with 10 million entries.
      • Methods inherited from class java.lang.Object

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

      • insertionSort

        public static <T> void insertionSort​(T[] arr,
                                             java.util.Comparator<T> compare)
        Public insertion sort implementation for sorting the entire array.
        Type Parameters:
        T - The type of elements in the array.
        Parameters:
        arr - The array to sort.
        compare - A comparator to compare the elements.
      • quicksort

        public static <T> void quicksort​(T[] arr,
                                         java.util.Comparator<T> compare)
        This algorithm beats Array.prototype.sort in Chrome only with arrays with 10 million entries. In most cases [].sort will do just fine. Make sure to performance test your use-case before you integrate this algorithm. Note that Chrome's sort is now a stable algorithm (Timsort). Quicksort is not stable.
        Type Parameters:
        T - The type of elements in the array.
        Parameters:
        arr - The array to sort.
        compare - A comparator to compare the elements.