Hi everybody,

I need some helpings to calculate time complexity(worst case, best case and average) for following code :

int m = A[0];
for (int i=1;i<n;i++) {if (A>m) m = A;}
int k = 0;
for(int i = 0; i<n;i++) {if(A == m) k++;}

Thank you ........................

Recommended Answers

All 3 Replies

worst case: run it on an old 80x88 computer that has 0.4 MG CPU chip, if you can find it.

best case: run it on a cray computer

average: average the two above

seriously: the timings will all depends on the os and CPU you run it on. And you may have to run that code several million times before you can get any readable timings. Then run that 1,000 times and you will get the worst case, best case and averages.

What are you talking about? The time complexity is a mathematical expression describing the growth rate of an algorithm's running time as an input parameter varies. Running an algorithm 1000 times wouldn't give you any clue about the time complexity.

yenh, I don't feel like writing out a tutorial every time this question occurs, so here's a link to some explanation from a previous thread. http://www.daniweb.com/techtalkforums/thread13488-2.html

Oh well, just ignore my ignorance. I had something else in mind. Good discussion in that link you posted. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.