Java Insertion Sort insertionSort(int[] input)

Here you can find the source of insertionSort(int[] input)

Description

1 for j = 2 to A.length 2 key = A[j] // Insert A[j] into the sorted sequence A [1 ..

License

Apache License

Parameter

Parameter Description
input a parameter

Declaration

public static int[] insertionSort(int[] input) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**// w  w  w.ja  v  a 2 s . co  m
     * 1 for j = 2 to A.length 
     * 2 key = A[j] 
     * // Insert A[j] into the sorted sequence A [1 .. j-1]. 
     * 3 i = j - 1 
     * 4 while i > 0 and A[i] > key 
     * 5 A[i+1] = A[i] 
     * 6 A[i] = key 
     * 7 i--
     * 
     * @param input
     * @return
     */
    public static int[] insertionSort(int[] input) {
        for (int j = 1; j < input.length; j++) {
            int key = input[j];
            int i = j - 1;
            while (i >= 0 && input[i] > key) {
                input[i + 1] = input[i];
                input[i] = key;
                i--;
            }
        }
        return input;
    }
}

Related

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