Java Insertion Sort insertionSort(int[] target, int[] coSort)

Here you can find the source of insertionSort(int[] target, int[] coSort)

Description

Sorts the specified array of ints into ascending order using insertion sort algorithm and simultaneously permutes the coSort ints array in the same way then specified target array.

License

Open Source License

Parameter

Parameter Description
target the array to be sorted
coSort the array, which will be permuted in the same way, then the specified target array, during sorting procedure

Exception

Parameter Description
IllegalArgumentException if coSort length less then target length.

Declaration

public static void insertionSort(int[] target, int[] coSort) 

Method Source Code

//package com.java2s;
/*//from w  ww. ja v a 2  s.  c  om
 * Redberry: symbolic tensor computations.
 *
 * Copyright (c) 2010-2013:
 *   Stanislav Poslavsky   <stvlpos@mail.ru>
 *   Bolotin Dmitriy       <bolotin.dmitriy@gmail.com>
 *
 * This file is part of Redberry.
 *
 * Redberry is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Redberry is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Redberry. If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Sorts the specified array of ints into ascending order using insertion sort algorithm and simultaneously permutes
     * the {@code coSort} ints array in the same way then specified target array. This sort guarantee O(n^2) performance
     * in the worst case and O(n) in the best case (nearly sorted input). <p/> <p> This sort is the best choice for
     * small arrays with elements number < 100. <p/> <p>This sort is guaranteed to be <i>stable</i>: equal elements will
     * not be reordered as a result of the sort; <i>adaptive</i>: performance adapts to the initial order of elements
     * and <i>in-place</i>: requires constant amount of additional space.
     *
     * @param target the array to be sorted
     * @param coSort the array, which will be permuted in the same way, then the specified target array, during sorting
     *               procedure
     * @throws IllegalArgumentException if coSort length less then target length.
     */
    public static void insertionSort(int[] target, int[] coSort) {
        insertionSort(target, 0, target.length, coSort);
    }

    /**
     * Sorts the specified array of ints into ascending order using insertion sort algorithm and simultaneously permutes
     * the {@code coSort} ints array in the same way then specified target array. This sort guarantee O(n^2) performance
     * in the worst case and O(n) in the best case (nearly sorted input). The range to be sorted extends from index
     * <tt>fromIndex</tt>, inclusive, to index <tt>toIndex</tt>, exclusive. (If <tt>fromIndex==toIndex</tt>, the range
     * to be sorted is empty.) <p/> <p> This sort is the best choice for small arrays with elements number < 100. <p/>
     * <p>This sort is guaranteed to be <i>stable</i>: equal elements will not be reordered as a result of the sort;
     * <i>adaptive</i>: performance adapts to the initial order of elements and <i>in-place</i>: requires constant
     * amount of additional space.
     *
     * @param target    the array to be sorted
     * @param fromIndex the index of the first element (inclusive) to be sorted
     * @param toIndex   the index of the last element (exclusive) to be sorted
     * @param coSort    the array, which will be permuted in the same way, then the specified target array, during
     *                  sorting procedure
     * @throws IllegalArgumentException       if <tt>fromIndex &gt; toIndex</tt>
     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or <tt>toIndex &gt; target.length</tt> or
     *                                        <tt>toIndex &gt; coSort.length</tt>
     */
    public static void insertionSort(int[] target, int fromIndex, int toIndex, int[] coSort) {
        rangeCheck(target.length, fromIndex, toIndex);
        rangeCheck(coSort.length, fromIndex, toIndex);

        int i, key, j, keyC;
        for (i = fromIndex + 1; i < toIndex; i++) {
            key = target[i];
            keyC = coSort[i];
            for (j = i; j > fromIndex && target[j - 1] > key; j--) {
                target[j] = target[j - 1];
                coSort[j] = coSort[j - 1];
            }
            target[j] = key;
            coSort[j] = keyC;
        }
    }

    /**
     * Sorts the specified array of ints into ascending order using insertion sort algorithm and simultaneously permutes
     * the {@code coSort} longs array in the same way then specified target array. This sort guarantee O(n^2)
     * performance in the worst case and O(n) in the best case (nearly sorted input). <p/> <p> This sort is the best
     * choice for small arrays with elements number < 100. <p/> <p>This sort is guaranteed to be <i>stable</i>: equal
     * elements will not be reordered as a result of the sort; <i>adaptive</i>: performance adapts to the initial order
     * of elements and <i>in-place</i>: requires constant amount of additional space.
     *
     * @param target the array to be sorted
     * @param coSort the array, which will be permuted in the same way, then the specified target array, during sorting
     *               procedure
     * @throws IllegalArgumentException if coSort length less then target length.
     */
    public static void insertionSort(int[] target, long[] coSort) {
        insertionSort(target, 0, target.length, coSort);
    }

    /**
     * Sorts the specified array of ints into ascending order using insertion sort algorithm and simultaneously permutes
     * the {@code coSort} ints array in the same way then specified target array. This sort guarantee O(n^2) performance
     * in the worst case and O(n) in the best case (nearly sorted input). The range to be sorted extends from index
     * <tt>fromIndex</tt>, inclusive, to index <tt>toIndex</tt>, exclusive. (If <tt>fromIndex==toIndex</tt>, the range
     * to be sorted is empty.) <p/> <p> This sort is the best choice for small arrays with elements number < 100. <p/>
     * <p>This sort is guaranteed to be <i>stable</i>: equal elements will not be reordered as a result of the sort;
     * <i>adaptive</i>: performance adapts to the initial order of elements and <i>in-place</i>: requires constant
     * amount of additional space.
     *
     * @param target    the array to be sorted
     * @param fromIndex the index of the first element (inclusive) to be sorted
     * @param toIndex   the index of the last element (exclusive) to be sorted
     * @param coSort    the array, which will be permuted in the same way, then the specified target array, during
     *                  sorting procedure
     * @throws IllegalArgumentException       if <tt>fromIndex &gt; toIndex</tt>
     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or <tt>toIndex &gt; target.length</tt> or
     *                                        <tt>toIndex &gt; coSort.length</tt>
     */
    public static void insertionSort(int[] target, int fromIndex, int toIndex, long[] coSort) {
        rangeCheck(target.length, fromIndex, toIndex);
        rangeCheck(coSort.length, fromIndex, toIndex);

        int i, key, j;
        long keyC;
        for (i = fromIndex + 1; i < toIndex; i++) {
            key = target[i];
            keyC = coSort[i];
            for (j = i; j > fromIndex && target[j - 1] > key; j--) {
                target[j] = target[j - 1];
                coSort[j] = coSort[j - 1];
            }
            target[j] = key;
            coSort[j] = keyC;
        }
    }

    /**
     * Sorts the specified target array of objects into ascending order, according to the natural ordering of its
     * elements using insertion sort algorithm and simultaneously permutes the {@code coSort} objects array in the same
     * way then specified target array. This sort guarantee O(n^2) performance in the worst case and O(n) in the best
     * case (nearly sorted input). <p/> <p> This sort is the best choice for small arrays with elements number < 100.
     * <p/> <p>This sort is guaranteed to be <i>stable</i>: equal elements will not be reordered as a result of the
     * sort; <i>adaptive</i>: performance adapts to the initial order of elements and <i>in-place</i>: requires constant
     * amount of additional space.
     *
     * @param target the array to be sorted
     * @param coSort the array, which will be permuted in the same way, then the specified target array, during sorting
     *               procedure
     * @throws IllegalArgumentException if coSort length less then target length.
     */
    public static <T extends Comparable<T>> void insertionSort(T[] target, Object[] coSort) {
        insertionSort(target, 0, target.length, coSort);
    }

    /**
     * Sorts the specified target array of objects into ascending order, according to the natural ordering of its
     * elements using insertion sort algorithm and simultaneously permutes the {@code coSort} objects array in the same
     * way then specified target array. This sort guarantee O(n^2) performance in the worst case and O(n) in the best
     * case (nearly sorted input). The range to be sorted extends from index <tt>fromIndex</tt>, inclusive, to index
     * <tt>toIndex</tt>, exclusive. (If <tt>fromIndex==toIndex</tt>, the range to be sorted is empty.) <p/> <p> This
     * sort is the best choice for small arrays with elements number < 100. <p/> <p>This sort is guaranteed to be
     * <i>stable</i>: equal elements will not be reordered as a result of the sort; <i>adaptive</i>: performance adapts
     * to the initial order of elements and <i>in-place</i>: requires constant amount of additional space.
     *
     * @param target    the array to be sorted
     * @param fromIndex the index of the first element (inclusive) to be sorted
     * @param toIndex   the index of the last element (exclusive) to be sorted
     * @param coSort    the array, which will be permuted in the same way, then the specified target array, during
     *                  sorting procedure
     * @throws IllegalArgumentException       if <tt>fromIndex &gt; toIndex</tt>
     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or <tt>toIndex &gt; target.length</tt> or
     *                                        <tt>toIndex &gt; coSort.length</tt>
     */
    public static <T extends Comparable<T>> void insertionSort(T[] target, int fromIndex, int toIndex,
            Object[] coSort) {
        int i, j;
        T key;
        Object keyC;
        for (i = fromIndex + 1; i < toIndex; i++) {
            key = target[i];
            keyC = coSort[i];
            for (j = i; j > fromIndex && target[j - 1].compareTo(key) > 0; j--) {
                target[j] = target[j - 1];
                coSort[j] = coSort[j - 1];
            }
            target[j] = key;
            coSort[j] = keyC;
        }
    }

    /**
     * Check that fromIndex and toIndex are in range, and throw an appropriate exception if they aren't.
     */
    private static void rangeCheck(int arrayLen, int fromIndex, int toIndex) {
        if (fromIndex > toIndex)
            throw new IllegalArgumentException("fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")");
        if (fromIndex < 0)
            throw new ArrayIndexOutOfBoundsException(fromIndex);
        if (toIndex > arrayLen)
            throw new ArrayIndexOutOfBoundsException(toIndex);
    }
}

Related

  1. insertionsort(double[] a, int[] b, int p, int r)
  2. insertionSort(int[] a)
  3. insertionSort(int[] a)
  4. insertionsort(int[] array)
  5. insertionSort(int[] input)
  6. insertionSortInPlaceSwap(int[] xs)