-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCalcUtils.java
82 lines (71 loc) · 2.36 KB
/
CalcUtils.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
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
package lab2_pro;
import java.util.Arrays;
public class CalcUtils {
public static void vectorInput(int[] vector, int filler) {
Arrays.fill(vector, filler);
}
public static void matrixInput(int[][] matrix, int filler) {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix.length; j++) {
matrix[i][j] = filler;
}
}
}
public static void bubbleSort(int[] vector, int start, int end) {
for (int i = start; i < end - 1; i++) {
for (int j = start + 1; j < end; j++) {
if (vector[i] > vector[j]) {
int tmp = vector[i];
vector[i] = vector[j];
vector[j] = tmp;
}
}
}
}
public static void mergeSort(int[] vector, int low, int high) {
if (low < high) {
int middle = (low / 2) + (high / 2);
mergeSort(vector, low, middle);
mergeSort(vector, middle + 1, high);
mergeParts(vector, low, middle, high);
}
}
private static void mergeParts(int[] vector, int low, int middle, int high) {
int left = low;
int right = middle + 1;
int[] tmp = new int[(high - low) + 1];
int tmpIndex = 0;
if (high != vector.length) {
while ((left <= middle) && (right <= high)) {
if (vector[left] < vector[right]) {
tmp[tmpIndex] = vector[left];
left = left + 1;
} else {
tmp[tmpIndex] = vector[right];
right = right + 1;
}
tmpIndex = tmpIndex + 1;
}
if (left <= middle) {
while (left <= middle) {
tmp[tmpIndex] = vector[left];
left = left + 1;
tmpIndex = tmpIndex + 1;
}
}
if (right <= high) {
while (right <= high) {
tmp[tmpIndex] = vector[right];
right = right + 1;
tmpIndex = tmpIndex + 1;
}
}
for (int i = 0; i < tmp.length; i++) {
vector[low + i] = tmp[i];
}
}
/* else {
System.out.println("kek");
}*/
}
}