import java.util.Arrays; public class InsertionSort { void sort(int array[]) { int l = array.length; for (int i = 1; i < l; ++i) { int val = array[i]; int j = i - 1; while (j >= 0 && array[j] > val) { array[j + 1] = array[j]; j = j - 1; } array[j + 1] = val; } } public static void main(String args[]) { int[] array1 = { 0, 9, 8, 7, 6, 5, 4, 3, 2, 1 }; System.out.println(Arrays.toString(array1)); InsertionSort ob = new InsertionSort(); ob.sort(array1); System.out.println(Arrays.toString(array1)); } }
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more