How to calculate time complexity of some nested loops with if statments [duplicate]

  c++, time-complexity

I understand the basics of how the complexity calculation works but I’m stuck for a couple of hours trying to figure out how to calculate this :

example 1:

for (i=1; i<n; i++)
{
    for (j=n-1; j>=i; j--)
        if (A[j]>A[j-1])
        {
            temp=A[j];
            A[j]=A[j-1];
            A[j-1]=temp;
        }
}
printf("%d", a[0]);

example 2:

m=A[0];
for (i=1; i<n; i++)
{
    If(A[i]>m)

    m=A[i];
}
printf("%d", m);

In which time complexity category each of the examples are? and which example is more efficient?

Source: Windows Questions C++

LEAVE A COMMENT