Output:
Click Here For Java Online Compiler
Solution:
*** Insertion Sort *** --- Array Before --- 23, 54, 67, 54, 12, 77, 48, 4, --- Array After --- 4, 12, 23, 48, 54, 54, 67, 77,
Click Here For Java Online Compiler
Solution:
/** * * @author Alee Ahmed Kolachi */ public class InsertionSort { public static int[] sortInsertion(int[] array) { int temporary; for (int i = 1; i < array.length; i++) { for (int j = i; j > 0; j--) { if (array[j] < array[j - 1]) { temporary = array[j]; array[j] = array[j - 1]; array[j - 1] = temporary; } } } return array; } public static void main(String a[]) { int[] array1 = {23, 54, 67, 54, 12, 77, 48, 4}; System.out.println("*** Insertion Sort ***" + "\n"); System.out.println("--- Array Before ---"); for (int i : array1) { System.out.print(i + ", "); } System.out.println("\n"); System.out.println("--- Array After ---"); int[] array2 = sortInsertion(array1); for (int i : array2) { System.out.print(i); System.out.print(", "); } } }
comment 0 comments:
more_vertsentiment_satisfied Emoticon