Insertion Sort Algorithm in Java
import java.util.Arrays; class Main { public static void insertion_Sort(int[] ar) { for (int i = 1; i < ar.length; i++) { int temp = ar[i]; int j = i; while (j > 0 && ar[j - 1] > temp) { ar[j] = ar[j - 1]; j--; } ar[j] = temp; } } public static void main(String[] args) { int[] arr = { 13, 81, -5, 14, 11, 9, -20 }; insertion_Sort(arr); // printing the sorted array System.out.println(Arrays.toString(arr)); } }
Output:-
[-20, -5, 9, 11, 13, 14, 81]