I have a recursive algorithm for the calculation of the weighted median. I'm trying to figure out what the Big-O time complexity will be but i am kinda stuck. Can someone help me please. Thank you all for the help. Here is the code in JAVA:
public static double WeightedMedian(ArrayList<Double> a1, ArrayList<Double> a2, int p, int r) {
if (r == p) {
return a1.get(p);
}
if (r-p == 0) {
if (a2.get(p) == a2.get(r)) {
return (a1.get(p) + a1.get(r))/2;
}
if (a2.get(p) > a2.get(r)) {
return a1.get(p);
} else {
return a1.get(r);}
}
long q = partition(a1, p, r);
double wl=0,wg=0;
for (int i=p; i<=q-1; i++) {
wl += a2.get(i);
}
for (int i=(int) (q+1); i<=r; i++) {
wg += a2.get(i);
}
if (wl<0.5 && wg<0.5) {
return a1.get((int)q);
} else {
if (wl > wg) {
double x = a2.get((int)q) + wg;
a2.set((int) q,x);
return WeightedMedian(a1,a2, p+1, (int)q);
} else {
double x = a2.get((int)q) + wl;
a2.set((int) q,x);
return WeightedMedian(a1, a2, (int)q, r);
}
}
sorry this is my first time posting here so im not really thta good i tried to format the code better but it kept going in strange places etc. Anyway partition code is as follows:
public static long partition (ArrayList<Double> arr, int low, int high)
{
double pivot = arr.get(high);
int i = low - 1;
for (int j = low; j <= high- 1; j++)
{
if (arr.get(j) <= pivot)
{
i++;
double temp = arr.get(i);
arr.set(i,arr.get(j));
arr.set(j,temp);
}
}
double temp1 = arr.get(i + 1);
arr.set(i + 1, arr.get(high));
arr.set(high,temp1);
return i + 1;
}
partitionmethod.