diff --git a/DSA_JAVA/InsertionSort.java b/DSA_JAVA/InsertionSort.java new file mode 100644 index 0000000..e7597f4 --- /dev/null +++ b/DSA_JAVA/InsertionSort.java @@ -0,0 +1,55 @@ +// Insertion sort in Java + +import java.util.Arrays; + +class InsertionSort { + + void insertionSort(int array[]) { + + int size = array.length; + + for (int step = 1; step < size; step++) { + + int key = array[step]; + + int j = step - 1; + + // Compare key with each element on the left of it until an element smaller than + + // it is found. + + // For descending order, change keyarray[j]. + + while (j >= 0 && key < array[j]) { + + array[j + 1] = array[j]; + + --j; + + } + + // Place key at after the element just smaller than it. + + array[j + 1] = key; + + } + + } + + // Driver code + + public static void main(String args[]) { + + int[] data = { 9, 5, 1, 4, 3 }; + + InsertionSort is = new InsertionSort(); + + is.insertionSort(data); + + System.out.println("Sorted Array in Ascending Order: "); + + System.out.println(Arrays.toString(data)); + + } + +}