简体   繁体   中英

I need help understanding running time and the worst case scenario

My professor was trying to get me to understand running time and worst case but I'm still confused. He say look at the for loop and see how many times it iterate and I guess for this code i have below it iterate n number of times. I just not too sure though. It for the Fibonacci sequence.

for (int i = 0; i < t; i++) {
        j[i] = q;
        int A = q;
        q = u;
        u = A + q;
    }

    for (int m = 0; m < b; m++) {
        if (j[m] <= b) {
            System.out.print(j[m]);
        }
    }

Yes. The complexity of the code is O(t) as the loop is running t times. Inside loop you are calculating the next fibonacci number and storing them into an array j[] .

Next you are printing the content of the array, which is also simple iteration over the array.

I would suggest you to always use relevant and meaningful variable name like n instead of t and b while looping. Also you should name the array properly like int fibonacci[] instead of int j[] . This type of code is always self explanatory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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