Class SortUtil


  • public final class SortUtil
    extends java.lang.Object
    Provides various sort utilities for a list of objects that implement Comparable. It also provide some methods that can be used on a sorted list of objects, such as a fast search method.

    All the methods in this class are static.

    Author:
    Tobias Downer
    • Constructor Summary

      Constructors 
      Constructor Description
      SortUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void quickSort​(java.lang.Comparable[] obs)
      Performs a quick sort on the given array of Comparable objects.
      static void quickSort​(java.lang.Comparable[] list, int min, int max)
      Performs a quick sort on the given array of Comparable objects between the min and maximum range.
      static int sortedIndexOf​(java.lang.Comparable[] list, java.lang.Comparable val, int lower, int higher)
      Quickly finds the index of the given object in the list.
      static SearchResults sortedQuickFind​(java.lang.Comparable[] list, java.lang.Comparable val, SearchResults results)
      Quickly finds the given element in the array of objects.
      • Methods inherited from class java.lang.Object

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

      • SortUtil

        public SortUtil()
    • Method Detail

      • quickSort

        public static void quickSort​(java.lang.Comparable[] list,
                                     int min,
                                     int max)
        Performs a quick sort on the given array of Comparable objects between the min and maximum range. It changes the array to the new sorted order.
      • quickSort

        public static void quickSort​(java.lang.Comparable[] obs)
        Performs a quick sort on the given array of Comparable objects. It changes the array to the new sorted order.
      • sortedIndexOf

        public static int sortedIndexOf​(java.lang.Comparable[] list,
                                        java.lang.Comparable val,
                                        int lower,
                                        int higher)
        Quickly finds the index of the given object in the list. If the object can not be found, it returns the point where the element should be added.
      • sortedQuickFind

        public static SearchResults sortedQuickFind​(java.lang.Comparable[] list,
                                                    java.lang.Comparable val,
                                                    SearchResults results)
        Quickly finds the given element in the array of objects. It assumes the object given is of the same type as the array. It returns null if the given object is not in the array. If the object is in the array, it returns a SearchResults object that contains information about the index of the found object, and the number of objects like this in the array. Not that it takes a 'SearchResults' object to store the results in. This is for reuse of an old SearchResults object that is no longer needed. This prevents gc and overhead of having to allocate a new SearchResults object. This method works by dividing the search space in half and iterating in the half with the given object in.