Assignment 3: insertion sort solve
This commit is contained in:
parent
1f3b5a4e84
commit
7fbc883a40
@ -6,8 +6,18 @@ import java.io.IOException;
|
|||||||
|
|
||||||
class Assignment {
|
class Assignment {
|
||||||
private static void insertionSort(int[] arr) {
|
private static void insertionSort(int[] arr) {
|
||||||
// Print the subarray at the end of each iteration to show invariant
|
for (int i = 1; i < arr.length; i++) {
|
||||||
printSubArray(arr, 1);
|
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
|
// Print the subarray arr[0, 1, ..., j] with ";" after elements
|
||||||
|
Loading…
x
Reference in New Issue
Block a user