forked from dr-cs/intro-oop-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSort.java
More file actions
29 lines (26 loc) · 820 Bytes
/
InsertionSort.java
File metadata and controls
29 lines (26 loc) · 820 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.util.Arrays;
public class InsertionSort {
public static void main(String[] args) {
int[] a = {5, 2, 4, 6, 1, 3};
System.out.println("Unsorted aray: " + Arrays.toString(a));
for (int j = 1; j < a.length; ++j) {
assert isSorted(a, 0, j - 1);
int key = a[j];
int i = j - 1;
while(i >= 0 && a[i] < key) {
a[i + 1] = a[i];
i = i - 1;
}
a[i + 1] = key;
}
System.out.println("Sorted aray: " + Arrays.toString(a));
}
private static boolean isSorted(int[] array, int start, int end) {
for (int i = start; i < end; ++i) {
if (!(array[i] < array[i + 1])) {
return false;
}
}
return true;
}
}