Assignment 3: insertion sort solve

This commit is contained in:
Yuri Tatishchev 2025-02-09 18:43:41 -08:00
parent 1f3b5a4e84
commit 7fbc883a40
Signed by: CaZzzer
GPG Key ID: E0EBF441EA424369

View File

@ -6,8 +6,18 @@ import java.io.IOException;
class Assignment {
private static void insertionSort(int[] arr) {
// Print the subarray at the end of each iteration to show invariant
printSubArray(arr, 1);
for (int i = 1; i < arr.length; i++) {
int val = arr[i];
int j = i-1;
while (j >= 0 && arr[j] > val) {
arr[j+1] = arr[j];
j--;
}
arr[j+1] = val;
// Print the subarray at the end of each iteration to show invariant
printSubArray(arr, i);
}
}
// Print the subarray arr[0, 1, ..., j] with ";" after elements