Class Insertion


  • public class Insertion
    extends Object
    The Insertion class provides static methods for sorting an array using insertion sort.

    In the worst case, this implementation makes ~ ½ n2 compares and ~ ½ n2 exchanges to sort an array of length n. So, it is not suitable for sorting large arbitrary arrays. More precisely, the number of exchanges is exactly equal to the number of inversions. So, for example, it sorts a partially-sorted array in linear time.

    This sorting algorithm is stable. It uses Θ(1) extra memory (not including the input array).

    See InsertionPedantic.java for a version that eliminates the compiler warning.

    For additional documentation, see Section 2.1 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.

    Author:
    Robert Sedgewick, Kevin Wayne
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int[] indexSort​(Comparable[] a)
      Returns a permutation that gives the elements in the array in ascending order.
      static void main​(String[] args)
      Reads in a sequence of strings from standard input; insertion sorts them; and prints them to standard output in ascending order.
      static void sort​(Comparable[] a)
      Rearranges the array in ascending order, using the natural order.
      static void sort​(Comparable[] a, int lo, int hi)
      Rearranges the subarray a[lo..hi) in ascending order, using the natural order.
      static void sort​(Object[] a, int lo, int hi, Comparator comparator)
      Rearranges the subarray a[lo..hi) in ascending order, using a comparator.
      static void sort​(Object[] a, Comparator comparator)
      Rearranges the array in ascending order, using a comparator.
    • Method Detail

      • sort

        public static void sort​(Comparable[] a)
        Rearranges the array in ascending order, using the natural order.
        Parameters:
        a - the array to be sorted
      • sort

        public static void sort​(Comparable[] a,
                                int lo,
                                int hi)
        Rearranges the subarray a[lo..hi) in ascending order, using the natural order.
        Parameters:
        a - the array to be sorted
        lo - left endpoint (inclusive)
        hi - right endpoint (exclusive)
      • sort

        public static void sort​(Object[] a,
                                Comparator comparator)
        Rearranges the array in ascending order, using a comparator.
        Parameters:
        a - the array
        comparator - the comparator specifying the order
      • sort

        public static void sort​(Object[] a,
                                int lo,
                                int hi,
                                Comparator comparator)
        Rearranges the subarray a[lo..hi) in ascending order, using a comparator.
        Parameters:
        a - the array
        lo - left endpoint (inclusive)
        hi - right endpoint (exclusive)
        comparator - the comparator specifying the order
      • indexSort

        public static int[] indexSort​(Comparable[] a)
        Returns a permutation that gives the elements in the array in ascending order.
        Parameters:
        a - the array
        Returns:
        a permutation p[] such that a[p[0]], a[p[1]], ..., a[p[n-1]] are in ascending order
      • main

        public static void main​(String[] args)
        Reads in a sequence of strings from standard input; insertion sorts them; and prints them to standard output in ascending order.
        Parameters:
        args - the command-line arguments