Faisal Saleem wrote:
> Dear All,
>  
> Considering the given code as an algorithm calculate the time complexity of 
> this code.
>  
> #include<iostream>
> #include<stdlib.h>
> using namespace std;
>  
> int main(){
>     int i, j, n;
> for(i=0;i<n; i++){
>  
>         for(j=0; j<n; j++){
>                 cout<<"my time complexity is = "<<i*j<<endl;
>                 }
>         cout<<"complexity is increasing"<<j<<endl;
>         }
> system("pause");
> return 0;
> }

O(N^2)

Probably.  But then I did quite poorly in ALL my theoretical classes in 
college.  My logic goes as thus:  You can talk theory all day long but 
practical application is the only thing that matters.  I can stare at 
the above loop and implicitly know that it will probably be expensive to 
run but it is only theoretical - it serves no real purpose.  It is 
similar to the discussion we had previously on this list:

char *str = "Some really long string - say 1MB........";

for (int x = 0; x < strlen(str); x++);

THAT is practical application.  There you have an opportunity to fix an 
actual problem.  And that is a common mistake made by all sorts of 
programmers.

-- 
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197

*NEW* MyTaskFocus 1.1
Get on task.  Stay on task.

http://www.CubicleSoft.com/MyTaskFocus/

Reply via email to