-
Notifications
You must be signed in to change notification settings - Fork 0
/
Inversion.java
84 lines (74 loc) · 2.38 KB
/
Inversion.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
83
84
import java.util.Arrays;
public class Inversion {
private int[] numbers;
private int[] helper;
private int number;
private int contador = 0;
private int cont = 0;
public void sort(int[] values) {
this.numbers = values;
number = values.length;
this.helper = new int[number];
mergesort(0, number - 1);
}
public void mergesort(int low, int high) {
// check if low is smaller then high, if not then the array is sorted
if (low < high) {
// Get the index of the element which is in the middle
int middle = low + (high - low) / 2;
// Sort the left side of the array
mergesort(low, middle);
// Sort the right side of the array
mergesort(middle + 1, high);
// Combine them both
merge(low, middle, high);
//cont = cont + contador;
}
}
public void merge(int low, int middle, int high){
// Copy both parts into the helper array
for (int i = low; i <= high; i++) {
helper[i] = numbers[i];
}
int i = low;
int j = middle + 1;
int k = low;
// Copy the smallest values from either the left or the right side back
// to the original array
while (i <= middle && j <= high) {
if (helper[i] <= helper[j]) {
numbers[k] = helper[i];
i++;
} else {
contador = contador + 1;
System.out.println(helper[i] + " > " + helper[j] + " inversiones parciales " + contador );
numbers[k] = helper[j];
j++;
}
k++;
}
// Copy the rest of the left side of the array into the target array
//System.out.println("Helpers: " + Arrays.toString(helper));
//System.out.println("Numbers: " + Arrays.toString(numbers));
while (i <= middle) {
if (numbers[k+1] < helper[k]){
contador = contador + 1;
System.out.println("Restante: "+ helper[i] + " > " + numbers[k] + " inversiones parciales " + contador );
}
numbers[k] = helper[i];
//System.out.println("Helper[i]" + helper[i] + " numbers[k] " + helper[k]);
k++;
i++;
}
}
public int contatoria (){
return contador;
}
public static void main(String[] args) {
int[] A = {38,16,27,39,12,72};
Inversion s = new Inversion();
s.sort(A);
System.out.println("INVERSION "+ Arrays.toString(A));
System.out.println("La cantidad de inversiones es: "+ s.contatoria());
}
}