package com.hackerrank.algorithms.arraysandsorting; import java.util.Scanner; /** * Created by IntelliJ IDEA. * User: ramswaroop * Date: 3/2/15 * Time: 3:26 PM * To change this template go to Preferences | IDE Settings | File and Code Templates */ public class LoopInvariant { public static void insertionSort(int[] A) { for (int i = 1; i < A.length; i++) { int value = A[i]; int j = i - 1; while (j >= 0 && A[j] > value) { A[j + 1] = A[j]; j = j - 1; } A[j + 1] = value; } printArray(A); } static void printArray(int[] ar) { for (int n : ar) { System.out.print(n + " "); } } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] ar = new int[n]; for (int i = 0; i < n; i++) { ar[i] = in.nextInt(); } insertionSort(ar); } }