Java Insertion Sort insertionSort(int[] a)

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

Description

Iterative implementation of a selection sort, which has Ο(n) total worst case performance.

License

Open Source License

Parameter

Parameter Description
a The array to sort using this algorithm.

Declaration

public static void insertionSort(int[] a) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//from ww  w  .  j av a 2 s. co m
     * Iterative implementation of a selection sort, which has Ο(n) 
     * total worst case performance. This implementation sorts in ascending 
     * order.
     * 
     * @param a The array to sort using this algorithm. 
     */
    public static void insertionSort(int[] a) {
        for (int i = 0; i < a.length; i++) {
            int temp = a[i]; // The element to insert.
            int nextIndex = i; // Next index of insertion candidate.
            while (nextIndex > 0 && temp < a[nextIndex - 1]) {
                a[nextIndex] = a[nextIndex - 1];
                nextIndex--;
            }
            a[nextIndex] = 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[] array)
  6. insertionSort(int[] input)
  7. insertionSort(int[] target, int[] coSort)