Question

I have a method in java that finds the longest non-decreasing segment in an array. The method works, however, as part of the assignment, I need to find the running time of size n elements using the method in terms of O(f(n))(i.e upper bound) and Ω(g(n))(i.e lower bound). Can someone help me? thanks in advance!!

    public int maxAscent(int A[])
    {
int num = 0;
int count = 1;
int i;
for(i = 0; i < A.length-1; i++)
    if(A[i] <= A[i+1])
        count++;
    else
    {
        if(count > num)
            num = count;
        count = 1;
    }
if(count > num)
    num = count;
return num;

}

Was it helpful?

Solution

The upper and lower bound, as well as the Big O have to do with the loops. The questions to ask are which loops do you have in your code? How many times does each loop run in the worst case and best case scenarios? If a loop contains another loop, you multiply their best cases and their worst cases.

For your particular program, there is one loop, and it goes from 0 to n-1. There is no early out so your best case, worst case, and average case all have the same value.

That would be "n" operations, so O(n), Ω(n) and ø(n).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top