-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSort.java
More file actions
33 lines (26 loc) · 876 Bytes
/
InsertionSort.java
File metadata and controls
33 lines (26 loc) · 876 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
30
31
32
33
public class InsertionSort {
public static void main(String[] args) {
String[] datasetFiles;
datasetFiles = Util.prepareDataset(Util.getDataSetDirectoryLocation());
Util.analyzeDataset(new InsertionSorter(), datasetFiles);
}
public static class InsertionSorter extends Sorter {
@Override
public void sort(Integer[] A) {
for (int i = 1; i < A.length; i++) {
int key = A[i];
int j = i - 1;
while (j >= 0) {
super.incrementComparisons(1);
if (A[j] > key) {
A[j + 1] = A[j];
j--;
}
else
break;
}
A[j + 1] = key;
}
}
}
}