-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathInsertionSortTest.java
More file actions
78 lines (65 loc) Β· 1.88 KB
/
InsertionSortTest.java
File metadata and controls
78 lines (65 loc) Β· 1.88 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
class InsertionSortTest
{
public static void asc()
{
int[] array = {-14, 5, 111, 44, 2, 9999, 99, 123, 1, -10, 33, -50};
for(int i = 1; i < array.length; i++){
int j = i;
while(j >= 1 && array[j-1] > array[j]) {
int tmp = array[j];
array[j] = array[j - 1];
array[j - 1] = tmp;
j--;
}
}
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
public static void desc()
{
int[] array = {-14, 5, 111, 44, 2, 9999, 99, 123, 1, -10, 33, -50};
for(int i = 1; i < array.length; i++){
int j = i;
while(j >= 1 && array[j - 1] < array[j]) {
int tmp = array[j];
array[j] = array[j - 1];
array[j - 1] = tmp;
j--;
}
}
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
public static void ascByParam(int[] array)
{
for(int i = 1; i < array.length; i++) {
int j = i;
while(j >= 1 && array[j - 1] > array[j]) {
int tmp = array[j];
array[j] = array[j - 1];
array[j - 1] = tmp;
j--;
}
}
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
public static void descByParam(int[] array)
{
for(int i = 1; i < array.length; i++){
int j = i;
while(j >= 1 && array[j - 1] < array[j]) {
int tmp = array[j];
array[j] = array[j - 1];
array[j - 1] = tmp;
j--;
}
}
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
}