Write a Java Program to insert sort 7, 1, 3, 2, 42, 76, 9, then write the sorted sublist for each step.
Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions. Thank You! =========================================================================== public class InsertionSort { public static void main(String a[]) { int[] arr = {7, 1, 3, 2, 42, 76, 9}; insertionSort(arr); } public static int[] insertionSort(int[] input) { int temp; for (int i = 1; i < input.length; i++) { for (int j = i; j > 0; j--) { if (input[j] < input[j - 1]) { temp = input[j]; input[j] = input[j - 1]; input[j - 1] = temp; } } display(input, i); } return input; } public static void display(int[] arr, int step) { System.out.print("Step #" + step+" >"); for (int num : arr) System.out.print(" " + num); System.out.println(); } }
=====================================================================
Get Answers For Free
Most questions answered within 1 hours.