-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweightedMedian.java
More file actions
143 lines (140 loc) · 4.11 KB
/
Copy pathweightedMedian.java
File metadata and controls
143 lines (140 loc) · 4.11 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package algorithmAssignments;
/**
* notice every time Median Algorithm put the smaller value to the front of RandomSelectedValue, put the bigger to the behind of
* RandomSelectedValue: to arrcord with average situation, select Median Algorithm. Attention: change the sequence of array.
*
* sum up the value of each of two part,if one is bigger than 1/2, select Median value from which, again, get two parts.
*
* input in the form of reading file
*/
import java.io.*;
public class weightedMedian {
public static int randomSpecifiedScopeInteger(int min, int max) {
int num = min + (int)(Math.random() * (max-min+1));
return num;
}
public static int[] swapArrayValue(int[] array, int i, int j) {
int temp;
temp = array[j];
array[j] = array[i];
array[i] = temp;
return array;
}
public static int[] MedianAlgorithm(int[] array, int startFrom, int endFrom) {
if(count == 0) {
length = endFrom - startFrom;
start = startFrom;
end = endFrom;
}
count++;
int boundary = randomSpecifiedScopeInteger(startFrom, endFrom); //index
int boundaryvalue = array[boundary];
int s = startFrom, e = endFrom;
while (s < e) {
if(array[e] == boundaryvalue) {
if(array[s] < boundaryvalue) {
s++;
continue;
}
if(array[s] > boundaryvalue) {
swapArrayValue(array, e, s);
continue;
}
}
if(array[s] == boundaryvalue) {
if(array[e] > boundaryvalue) {
e--;
continue;
}
if(array[e] < boundaryvalue) {
swapArrayValue(array, e, s);
continue;
}
}
if(array[s] > boundaryvalue) {
swapArrayValue(array, s, e);
e--;
}
if(array[s] < boundaryvalue) {
s++;
}
}
if(s < (int) (start + end) / 2) {
return MedianAlgorithm(array, s, endFrom);
}
if(s > (int) (start + end) / 2) {
return MedianAlgorithm(array, startFrom, e);
}
else {
medianIndex = s;
medianNumber = array[medianIndex];
count = 0;
end = -1;
start = -1;
return array;
}
}
public static int[] WeightedMedian(int[] array, int startFrom, int endFrom) {
float leftWeight = 0;
float rightWeight = 0;
x = MedianAlgorithm(array, startFrom, endFrom);
for (int i = 0; i < medianIndex; i++) {
leftWeight = leftWeight + w[x[i]];
}
for (int i = x.length - 1; i > medianIndex; i--) {
rightWeight = rightWeight + w[x[i]];
}
if (leftWeight > 0.5) {
return WeightedMedian(x, startFrom, medianIndex);
}
if (rightWeight > 0.5){
return WeightedMedian(x, medianIndex, endFrom);
}
else { // (leftWeight < 0.5 && rightWeight < 0.5)
System.out.println("Index: " + medianIndex);
System.out.println("x[i]: " + x[medianIndex] + " w[x[i]]: " + w[x[medianIndex]]);
return x;
}
}
static int start = -1;
static int end = -1;
static int medianIndex = -1;
static int medianNumber = -1;
static int length = -1;
static int count = 0; // MedianAlgorithm
static int times = 0; // WeightedMedian
static String line = null;
static String[] sp;
static int[] x = null;
static float[] w = null;
static int[] t = null;
static int lineNumber = 0;
static float judge = 0;
public static void main(String[] args) throws Exception {
FileReader fr=new FileReader("D:\\default\\testJavaInput\\first\\2.txt");
BufferedReader br=new BufferedReader(fr);
while((line=br.readLine())!=null){
lineNumber++;
weightedMedian.sp = line.split(" ");
if(lineNumber%3 == 1) {
weightedMedian.x = new int[weightedMedian.sp.length];
for(int i = 0, l = weightedMedian.sp.length; i < l; ++i){
weightedMedian.x[i] = Integer.parseInt(weightedMedian.sp[i]);
}
}
if(lineNumber%3 == 2) {
weightedMedian.w = new float[sp.length];
for(int i = 0; i < sp.length; ++i) {
w[x[i]] = Float.parseFloat(sp[i]);
judge = judge + w[x[i]];
}
}
}
br.close();
if(judge != 1) {
System.out.println("input weight is not 1");
return;
}
WeightedMedian(x, 0, x.length - 1);
}
}