简体   繁体   English

递归算法的大O复杂度

[英]Big O complexity of recursive algorithm

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.我试图弄清楚 Big-O 时间复杂度是多少,但我有点卡住了。 Can someone help me please.有人能帮助我吗。 Thank you all for the help.谢谢大家的帮助。 Here is the code in JAVA:这是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;
}

Mathematical description of weighted median加权中位数的数学描述

public static double WeightedMedian(ArrayList<Double> a1, ArrayList<Double> a2, int p, int r) {
if (r == p) {
    return a1.get(p); //O(1)
}

if (r-p == 0) {
    if (a2.get(p) == a2.get(r)) { //O(1) + O(1)
        return (a1.get(p) + a1.get(r))/2; //O(1) + O(1)
}
if (a2.get(p) > a2.get(r)) { //O(1) + O(1)
    return a1.get(p); //O(1)
} else {
    return a1.get(r);} //O(1)
}

long q = partition(a1, p, r);  
double wl=0,wg=0;
for (int i=p; i<=q-1; i++) { //O(n)
    wl += a2.get(i); 
}
for (int i=(int) (q+1); i<=r; i++) { //O(n)
    wg += a2.get(i); 
}
if (wl<0.5 && wg<0.5) { 
    return a1.get((int)q); //O(1)
} else {
    if (wl > wg) { 
        double x = a2.get((int)q) + wg; //O(1)
        a2.set((int) q,x); //O(1)
        return WeightedMedian(a1,a2, p+1, (int)q);
    } else { 
       double x = a2.get((int)q) + wl; //O(1)
       a2.set((int) q,x); //O(1)
       return WeightedMedian(a1, a2, (int)q, r);
    }
}

So the above method is O(n), from O(1)...+O(n) = O(n)所以上面的方法是O(n),从O(1)...+O(n) = O(n)

public static long partition (ArrayList<Double> arr, int low, int high)  {
double pivot = arr.get(high); //O(1)

int i = low - 1;

for (int j = low; j <= high- 1; j++) //O(n)
{
    if (arr.get(j) <= pivot) //O(1)
    {
        i++;
        double temp = arr.get(i); //O(1)
        arr.set(i,arr.get(j)); //O(1)
        arr.set(j,temp); //O(1)
    }
}
double temp1 = arr.get(i + 1); //O(1)
arr.set(i + 1, arr.get(high)); //O(1)
arr.set(high,temp1); //O(1)

return i + 1;
}

And the above method "partition", also O(n), derived by O(1)... + O(n)而上面的方法“分区”,也是O(n),由O(1)... + O(n)导出

So, O(n), as arraylist is direct access with index's and all get/sets are O(1) and all of your loops are O(n)所以,O(n),因为 arraylist 是直接访问索引的,所有获取/集都是 O(1),所有循环都是 O(n)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM