-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSort.java
More file actions
41 lines (32 loc) · 856 Bytes
/
InsertionSort.java
File metadata and controls
41 lines (32 loc) · 856 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
34
35
36
37
38
39
40
41
public class InsertionSort {
static int comparisons = 0;
public static void main(String[] args) {
int[] A = { 5, 3, 8, 9, 1, 7, 0, 2, 6, 4 };
System.out.println("Before Insertion Sort");
for (int i = 0; i < A.length; i++) {
System.out.print(A[i] + " ");
}
System.out.print("\n");
int[] sorted = InsertionSort(A);
System.out.println("After Insertion Sort");
for (int i = 0; i < sorted.length; i++) {
System.out.print(sorted[i] + " ");
}
System.out.print("\n");
System.out.println("Total Comparisons: " + comparisons);
}
public static int[] InsertionSort(int[] arr) {
int temp;
for (int i = 1; i < arr.length; i++) {
for (int j = i; j > 0; j--) {
if (arr[j] < arr[j - 1]) {
comparisons++;
temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
}
}
}
return arr;
}
}