-
Notifications
You must be signed in to change notification settings - Fork 0
/
InsertionSortGeneric.java
54 lines (50 loc) · 1.65 KB
/
InsertionSortGeneric.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
52
53
54
public class InsertionSortGeneric<T extends Comparable<T>> {
public void insertionsort_asc(T[] arr) {
for (int i = 1; i < arr.length; i++) {
T temp = arr[i];
int j = i - 1;
while (j >= 0 && arr[j].compareTo(temp) > 0) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = temp;
}
}
public void insertionsort_desc(T[] arr) {
for (int i = 1; i < arr.length; i++) {
T temp = arr[i];
int j = i - 1;
while (j >= 0 && arr[j].compareTo(temp) < 0) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = temp;
}
}
public static void main(String[] args) {
InsertionSortGeneric<Integer> is = new InsertionSortGeneric<>();
Integer[] arr = {1,4,3,5,2};
is.insertionsort_asc(arr);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
}
System.out.println();
is.insertionsort_desc(arr);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
}
InsertionSortGeneric<String> is1 = new InsertionSortGeneric<>();
String str[]={"Pepsi","Coca-cola","Limka","Fanta","Sprite"};
is1.insertionsort_asc(str);
System.out.println();
for (int i = 0; i < str.length; i++) {
System.out.print(str[i]+" ");
}
System.out.println();
is1.insertionsort_desc(str);
System.out.println();
for (int i = 0; i < str.length; i++) {
System.out.print(str[i]+" ");
}
}
}