diff --git a/Java/insertionSort.java b/Java/insertionSort.java new file mode 100644 index 0000000..1777fec --- /dev/null +++ b/Java/insertionSort.java @@ -0,0 +1,35 @@ +public class InsertionSort { + public static void insertionSort(int[] array) { + int n = array.length; + for (int i = 1; i < n; i++) { + int key = array[i]; + int j = i - 1; + + // Move elements of array[0..i-1], that are greater than key, to one position ahead of their current position + while (j >= 0 && array[j] > key) { + array[j + 1] = array[j]; + j = j - 1; + } + array[j + 1] = key; + } + } + + public static void main(String[] args) { + int[] array = { 12, 11, 13, 5, 6 }; + + System.out.println("Original array:"); + printArray(array); + + insertionSort(array); + + System.out.println("Sorted array:"); + printArray(array); + } + + public static void printArray(int[] array) { + for (int i : array) { + System.out.print(i + " "); + } + System.out.println(); + } +}