-
Notifications
You must be signed in to change notification settings - Fork 72
/
SortingCrads.java
51 lines (44 loc) · 1.43 KB
/
SortingCrads.java
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
public class SortingCrads {
public static void main(String[] args) {
//initializing array
int[] array = {34, 02, 13, 25, 43, 12, 04, 21};
Greencard(array);
}
//sorting green cards
private static void Greencard(int[] array) {
for (int m = 0; m < array.length; m++) {
int key = array[m];
int n = m - 1;
while (n>=0 && key%10<array[n]%10){
array[n+1] = array[n];
--n;
}
array[n+1] = key;
}
//get the output of sorted green cards
System.out.println("Sorted by green cards:");
for (int x = 0; x < array.length; x++) {
System.out.printf("%02d ", array[x]);
}
System.out.print("\n");
Redcard(array);
}
//sorting red cards
public static void Redcard(int[] array) {
for (int m = 1; m < array.length; m++) {
int key = array[m];
int n = m - 1;
while (n>=0 && key<array[n]){
array[n+1] = array[n];
--n;
}
array[n+1] = key;
}
//get the output of sorted red cards
System.out.println("Sorted by red cards: ");
for (int x = 0; x < array.length; x++) {
System.out.printf("%02d ", array[x]);
}
System.out.print("\n");
}
}