insertion sorot
package sorting; import java.util.Arrays; public class InsertionSort { public static void main(String[] args) { int[] arr = new int[]{5, 4, 3, 2, 1}; insertionSort(arr); System.out.println(Arrays.toString(arr)); } /** * run till current element is smaller * copy the left element to their right * now after loop break insert the element * @param arr */ static void insertionSort(int[] arr) { for (int i = 1; i < arr.length; i++) { int j = i - 1; int key = arr[i]; while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j--; } arr[j + 1] = key; } } }