import java.util.Random;

public class InsertionSort {
	
	public static void sort(int[] array) {
		for (int i = 1; i < array.length; i++) {
			int key = array[i];
			int j = i - 1;
			while (j >= 0 && array[j] > key) {
				array[j + 1] = array[j];
				j--;
			}
			array[j + 1] = key;
		}
	}

	public static int[] generateIntArray(int size) {
		//how to shuffle
		int[] array = new int[size];
		for (int i = 0; i < size; i++) {
			array[i] = i + 1;
		}
		shuffle(array);
		return array;
	}

	public static boolean isSorted(int[] array) {
		for (int i = 0; i < array.length - 1; i++) {
			if (array[i] > array[i + 1])
				return false;
		}
		return true;
	}

	public static void shuffle(int[] array) {
		Random r = new Random();
		for (int i = array.length - 1; i > 0; i--) {
			swap(array, i, r.nextInt(i));
		}
	}

	public static void printArray(int[] array) {
		System.out.print("[");
		for (int i: array) {
			System.out.printf(i + ", ");
		}
		System.out.println("]");
	}

	//swap two elements of indices
	public static void swap(int[] array, int idx1, int idx2) {
		int temp = array[idx1];
		array[idx1] = array[idx2];
		array[idx2] = temp;
	}

	//main
	public static void main(String[] args) {
		int[] arr = generateIntArray(20);
		printArray(arr);

		sort(arr);

		//result
		if (isSorted(arr)) {
			System.out.println("Sorted Well");
			printArray(arr);
		} else {
			System.out.println("Something Wrong");
		}
	}
}

@@ -1189,24 +1189,58 @@
 g%5B%5D args) %7B%0A
+%09%09for (int i = 0; i %3C 10; i++) %7B%0A%09
 %09%09int%5B%5D arr 
@@ -1265,16 +1265,17 @@
 (20);%0A%09%09
+%09
 printArr
@@ -1284,16 +1284,17 @@
 (arr);%0A%0A
+%09
 %09%09sort(a
@@ -1301,16 +1301,17 @@
 rr);%0A%0A%09%09
+%09
 //result
@@ -1311,16 +1311,17 @@
 /result%0A
+%09
 %09%09if (is
@@ -1331,24 +1331,46 @@
 ted(arr)) %7B%0A
+%09%09%09%09printArray(arr);%0A%09
 %09%09%09System.ou
@@ -1396,35 +1396,16 @@
 ell%22);%0A%09
-%09%09printArray(arr);%0A
 %09%09%7D else
@@ -1407,16 +1407,17 @@
  else %7B%0A
+%09
 %09%09%09Syste
@@ -1446,21 +1446,26 @@
 ng Wrong%22);%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%09%7D%0A%7D%0A

Update code