-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest.java
More file actions
32 lines (32 loc) · 912 Bytes
/
Test.java
File metadata and controls
32 lines (32 loc) · 912 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
/**
* @date 2022/11/21 19:57:30
* @description
*/
public class Test {
public static void main(String[] args) {
int[] a = {3, 2, 4, 1, 5};
//selectSort(a);
insertSort(a);
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
public static void insertSort(int[] a) {
for (int i = 1; i < a.length; i++) {
for(int j = i - 1; j >= 0; j--) {
if(a[j + 1] < a[j]) {
TestSort.swap(a, j + 1, j);
}
}
}
}
public static void selectSort(int[] a) {
for (int i = 0; i < a.length; i++) {
int minIndex = i;
for (int j = i + 1; j < a.length; j++) {
minIndex = a[j] < a[minIndex] ? j : minIndex;
}
TestSort.swap(a, i, minIndex);
}
}
}