-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArraySorts.java
More file actions
84 lines (69 loc) · 2.18 KB
/
ArraySorts.java
File metadata and controls
84 lines (69 loc) · 2.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package algorithms.sorts;
public class ArraySorts {
public static int numberOfCompares = -1;
public static int numberOfSwaps = -1;
public static <E extends Comparable<E>> void selectionSort(E[] array) {
numberOfCompares = 0;
numberOfSwaps = 0;
for (int from = 0; from < array.length; from++) {
E minimum = array[from];
int indexMinimum = from;
for (int i = from + 1; i < array.length; i++) {
numberOfCompares++;
if (minimum.compareTo(array[i]) > 0) {
minimum = array[i];
indexMinimum = i;
}
}
// swap
swap(array, from, indexMinimum);
numberOfSwaps++;
}
}
private static void swap(Object[] array, int i, int j) {
Object temp = array[i];
array[i] = array[j];
array[j] = temp;
}
public static <E extends Comparable<E>> void bubbleSort(E[] array) {
numberOfCompares = 0;
numberOfSwaps = 0;
int unSortedLength = array.length;
while (unSortedLength > 1) {
for (int i = 1; i < unSortedLength; i++) {
numberOfCompares++;
if (array[i].compareTo(array[i - 1]) < 0) {
E temp = array[i];
array[i] = array[i - 1];
array[i - 1] = temp;
numberOfSwaps++;
}
}
unSortedLength--;
}
}
/**
*
* @param <E>
* @param array
*/
//TODO rewrite it with shifting from left to right
public static <E extends Comparable<E>> void insertionSort(E[] array) {
numberOfCompares = 0;
numberOfSwaps = 0;
for (int i = 1; i < array.length; i++) {
E temp = array[i];
for (int j = i; j > 0; j--) {
numberOfCompares++;
if (temp.compareTo(array[j - 1]) < 0) {
array[j] = array[j - 1];
array[j - 1] = temp;
} else {
break;
}
}
}
}
private ArraySorts() {
}
}