Here you can find the source of insertionSort(int[] a)
Parameter | Description |
---|---|
a | The array to sort using this algorithm. |
public static void insertionSort(int[] a)
//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; } } }