![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Sep 2006
Posts: 29
Rep Power: 0
![]() |
algorithm complexity
hey guys,
can anyone give me a way to determine the complexity of an algorithm in the big o notation?? i will give an example and please provide me with the way that i should do it, thanks.
for(cnt = 0, i=0; i < n; i *= 2 )
for(j=0; j<n; j++ )
cnt++;also this is another example:
for(cnt=0, i=0; j<n; i++)
for(j=0; j <= i; j++)
cnt++;thanks |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
|
I am pretty sure the complexity of the first one is O(log_2(n))
and the second one is O(n^2). |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Sep 2006
Posts: 29
Rep Power: 0
![]() |
but how can determine it??
|
|
|
|
|
|
#4 |
|
Hobbyist Programmer
|
you determine it by how it grows. For instance you have a nested for statements like this:
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
//some statements;
}
}In your first example you have i *= 2, which is not linear, and when you have something growing like i *= 2 the complexity is log(n). Simple statements, like int i = a + b, are of complexity 1. Another example: for(int i = 0; i < n; i++){//complexity n
int tm += i;//complexity 1
}and the complexity is the highest power of the equation, in this case n, so complexity is O(n). Hope this helps, you also may want to google it to get a better description of it. |
|
|
|
|
|
#5 |
|
Programmer
|
You can have further expanation of determining compexity of algorithums from the following URL http://www.cs.bham.ac.uk/~mhe/foundations2/node121.html
__________________
Iftikhar Ahmed Khan For doing an experiment on programmer's mood please visit http://uxisfyp1.brunel.ac.uk/cspgiak |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Oct 2005
Posts: 134
Rep Power: 4
![]() |
Actually your first code results in an infinite loop when n > 0. The outer loop mulitplies i by 2 at the end, and since i starts at 0 and 0 * 2 == 0, i will be always be 0.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| prim's algorithm | cwl157 | C++ | 7 | Apr 18th, 2006 9:45 PM |
| algorithm of fast exponentiation | MicRo | C++ | 1 | Mar 12th, 2006 6:29 PM |
| SpcFileWipe algorithm in Secure Programming Cookbook not working | c0ldshadow | C++ | 1 | Aug 7th, 2005 8:40 PM |
| rsa encrption decrption algorithm | justdoit | C | 2 | May 4th, 2005 10:16 AM |
| Help with some algorithm questions | mapster123 | Assembly | 3 | Feb 14th, 2005 5:55 AM |