Question

void subelement ( int n, const array S[]) { if(n<3) { print("Not enough elements for subset")...

void subelement ( int n, const array S[])

{

if(n<3)

{

print("Not enough elements for subset")

}

index i, j, k;

for( i =1; i <=n; i++)

{

for(j=i+1; j<=n; j++)

{

for (k = j +1; k<=n;k++)

{

print(S[i],S[j],S[k])

}

}

}

}

Is the time complexity O(n^3)??

is there an worst case complexity?

0 0
Add a comment Improve this question Transcribed image text
Answer #1
void subelement(int n, const array S[]) {
    if (n < 3) {
        print("Not enough elements for subset")
    }
    index i, j, k;
    for (i = 1; i <= n; i++) {  // iterates n times
        for (j = i + 1; j <= n; j++) {  // iterates j=O(n) times
            for (k = j + 1; k <= n; k++) {  // iterates k=O(n) times
                print(S[i], S[j], S[k])
            }
        }
    }
}

total number of iterations = n*n*n = n^3
so, time complexity is O(n^3)

Yes, It has worst case time complexity. and it is O(n^3)
Add a comment
Know the answer?
Add Answer to:
void subelement ( int n, const array S[]) { if(n<3) { print("Not enough elements for subset")...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT