Untitled
unknown
plain_text
a year ago
1.2 kB
4
Indexable
Never
import java.util.*; import java.io.*; public class Main { static void insertionSort1(int a[], int n) { // Store the value of a[n-1] in a variable int key = a[n-1]; int j = n - 2; // Test lower index values successively from N-2 to 0 while (j >= 0 && a[j] > key) { // If the current value is greater than e, // shift the current value to the right a[j+1] = a[j]; j--; // Print the current state of the array for (int i = 0; i < n; i++) { System.out.print(a[i] + " "); } System.out.println(); } // Insert the stored value at the current index a[j+1] = key; // Print the final state of the array for (int i = 0; i < n; i++) { System.out.print(a[i] + " "); } System.out.println(); } public static void main(String args[]) { Scanner input = new Scanner(System.in); int n = input.nextInt(); int a[] = new int[n]; for(int i = 0; i < n; i++){ a[i] = input.nextInt(); } // Call the insertionSort1 function insertionSort1(a, n); } }