Java Insertion Sort insertionsort(int[] array)

Here you can find the source of insertionsort(int[] array)

Description

insertionsort

License

Open Source License

Declaration

public static void insertionsort(int[] array) 

Method Source Code

//package com.java2s;
/**//from w w  w .  j a v  a2s.c  om
 * Copyright (c) 2014-2015 by Wen Yu.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Any modifications to this file must keep this entire header intact.
 * 
 * Change History - most recent changes go on top of previous changes
 *
 * ArrayUtils.java
 *
 * Who   Date       Description
 * ====  =========  ======================================================================
 * WY    06Apr2015  Added reverse(byte[]) to reverse byte array elements
 * WY    06Jan2015  Added reverse() to reverse array elements
 * WY    10Dec2014  Moved reverseBits() from IMGUtils to here along with BIT_REVERSE_TABLE
 * WY    08Dec2014  Fixed bug for flipEndian() with more than 32 bit sample data 
 * WY    07Dec2014  Changed method names for byte array to other array types conversion
 * WY    07Dec2014  Added new methods to work with floating point TIFF images
 * WY    03Dec2014  Added byteArrayToFloatArray() and byteArrayToDoubleArray()
 * WY    25Nov2014  Added removeDuplicates() to sort and remove duplicates from int arrays
 * WY    12Nov2014  Changed the argument sequence for flipEndian()
 * WY    11Nov2014  Changed flipEndian() to include scan line stride to skip bits
 * WY    11Nov2014  Added toNBits() to convert byte array to nBits data unit
 * WY    28Oct2014  Added flipEndian() to work with TIFTweaker mergeTiffImagesEx()
 */

public class Main {
    public static void insertionsort(int[] array) {
        insertionsort(array, 0, array.length - 1);
    }

    public static void insertionsort(int[] array, int start, int end) {
        int j;

        for (int i = start + 1; i < end + 1; i++) {
            int temp = array[i];
            for (j = i; j > start && temp <= array[j - 1]; j--)
                array[j] = array[j - 1];
            // Move temp to the right place
            array[j] = temp;
        }
    }

    public static <T extends Comparable<? super T>> void insertionsort(T[] array, int start, int end) {
        int j;

        for (int i = start + 1; i < end + 1; i++) {
            T temp = array[i];
            for (j = i; j > start && temp.compareTo(array[j - 1]) <= 0; j--)
                array[j] = array[j - 1];
            // Move temp to the right place
            array[j] = temp;
        }
    }
}

Related

  1. insertionSort(Comparable[] a, int low, int high)
  2. insertionSort(double[] a, int low, int high)
  3. insertionsort(double[] a, int[] b, int p, int r)
  4. insertionSort(int[] a)
  5. insertionSort(int[] a)
  6. insertionSort(int[] input)
  7. insertionSort(int[] target, int[] coSort)
  8. insertionSortInPlaceSwap(int[] xs)