Click here to Skip to main content
15,909,466 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey guys, i wish for good and clear explanation for this program complexity(space&time)
thx.

C#
#include <stdio.h>

void main()
{
    char s[50]="Crrosssection";
    int i,k=0;
    char res[50];

    for(i=0;s[i]!=0;i++){
          if(s[i]!=s[i+1]) res[k]=s[i],k+=1; 
    }
}
Posted
Updated 6-Feb-13 6:04am
v4
Comments
CHill60 6-Feb-13 12:00pm    
It's not clear what your question is

The space complexity O(2n) where n is the string buffer size.
The time complexity is O(m) where m is the actual size of the string.

BTW, what will happen if the string assigned to the buffer is more than 49 characters long?

Cheers!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 6-Feb-13 18:39pm    
My 5, and I can answer your last question:
In this case, the code will behave as having the time complexity of O(1) :-)
—SA
Manfred Rudolf Bihy 7-Feb-13 8:15am    
Thanks Sergey!

And yes O(1) it should be, not that the program would do any significant work though! :)
Sergey Alexandrovich Kryukov 7-Feb-13 8:51am    
Big O notation says nothing about usefulness of the results of calculation. :-)
Cheers,
—SA
If you have Visual Studio Team System Installed it comes with Code Complexity Analysis tools. there are many free tools available for code complexity analysis try this one[^] or google [^] it for more tools.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900