-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInsertionSort.java
More file actions
54 lines (41 loc) · 1.18 KB
/
InsertionSort.java
File metadata and controls
54 lines (41 loc) · 1.18 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
public class InsertionSort {
private int shifts;
public int[] swap(int[] array, int i, int j) {
int t = array[i];
array[i] = array[j];
array[j] = t;
return array;
}
public int[] shift(int[] array, int i) {
if (i == 0)
return array;
while (i > 0) {
if (array[i] < array[i - 1]) {
array = swap(array, i, i - 1);
i--;
shifts++;
} else
break;
}
return array;
}
public int[] sort(int[] array, int i) {
if (i == 0)
return array;
sort(array, i - 1);
shift(array, i - 1);
return array;
}
public static void main(String[] args) {
//int[] array = new int[] {4, 3, 2, 1};
//int[] array = new int[] {1, 1, 1, 2, 2};
int[] array = new int[] {2, 1, 3, 1, 2};
InsertionSort is = new InsertionSort();
array = is.sort(array, array.length);
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
System.out.println("Shifts: " + is.shifts);
}
}