473,405 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

complexity of an algorithm

How do we calculate the complexity of an algorithm?

Am i right if i say the complexity of an algorithm
is the number of comparisons done in that algorithm?

thanx in advance .......
Nov 14 '05 #1
5 3910
ju**********@yahoo.co.in (junky_fellow) writes:
How do we calculate the complexity of an algorithm?

Am i right if i say the complexity of an algorithm
is the number of comparisons done in that algorithm?


Your question is outside the domain of comp.lang.c, which discusses
only the standard C programming language, including the standard C
library. This is a remarkably narrow topic compared to what many
people expect.

For your convenience, the list below contains topics that are not
on-topic for comp.lang.c, and suggests newsgroups for you to explore
if you have questions about these topics. Please do observe proper
netiquette before posting to any of these newsgroups. In particular,
you should read the group's charter and FAQ, if any (FAQs are
available from www.faqs.org and other sources). If those fail to
answer your question then you should browse through at least two weeks
of recent articles to make sure that your question has not already
been answered.

* OS-specific questions, such as how to clear the screen,
access the network, list the files in a directory, or read
"piped" output from a subprocess. These questions should be
directed to OS-specific newsgroups, such as
comp.os.ms-windows.programmer.misc, comp.unix.programmer, or
comp.os.linux.development.apps.

* Compiler-specific questions, such as installation issues and
locations of header files. Ask about these in
compiler-specific newsgroups, such as gnu.gcc.help or
comp.os.ms-windows.programmer.misc. Questions about writing
compilers are appropriate in comp.compilers.

* Processor-specific questions, such as questions about
assembly and machine code. x86 questions are appropriate in
comp.lang.asm.x86, embedded system processor questions may
be appropriate in comp.arch.embedded.

* ABI-specific questions, such as how to interface assembly
code to C. These questions are both processor- and
OS-specific and should typically be asked in OS-specific
newsgroups.

* Algorithms, except questions about C implementations of
algorithms. "How do I implement algorithm X in C?" is not a
question about a C implementation of an algorithm, it is a
request for source code. Newsgroups comp.programming and
comp.theory may be appropriate.

* Making C interoperate with other languages. C has no
facilities for such interoperation. These questions should
be directed to system- or compiler-specific newsgroups. C++
has features for interoperating with C, so consider
comp.lang.c++ for such questions.

* The C standard, as opposed to standard C. Questions about
the C standard are best asked in comp.std.c.

* C++. Please do not post or cross-post questions about C++
to comp.lang.c. Ask C++ questions in C++ newsgroups, such
as comp.lang.c++ or comp.lang.c++.moderated.

* Test posts. Please test in a newsgroup meant for testing,
such as alt.test.

news.groups.questions is a good place to ask about the appropriate
newsgroup for a given topic.
Nov 14 '05 #2
In article <8c**************************@posting.google.com >,
ju**********@yahoo.co.in (junky_fellow) wrote:
How do we calculate the complexity of an algorithm?

Am i right if i say the complexity of an algorithm
is the number of comparisons done in that algorithm?


Function f () calculates the sum of 1/x for x = 1 to 1e12 without any
comparisons:

double f1 (double x) { return 1/x + 1/(x+1) + 1/(x+2) ... + 1/(x+9); }
double f2 (double x) { return f1(x) + f1(x+10) + f1(x+20)...+ f1(x+90); }
double f3 (double x) { return f2(x) + f2(x+100)...+f2 (x+900); }
....
double f12(double x) { return f11(x) + f11(x+1e11) + ... + f11(x+9e11); }

double f (void) { return f12 (1.0); }

So the answer to your question seems to be "no".
Nov 14 '05 #3
junky_fellow <ju**********@yahoo.co.in> scribbled the following:
How do we calculate the complexity of an algorithm? Am i right if i say the complexity of an algorithm
is the number of comparisons done in that algorithm? thanx in advance .......


This has pretty much nothing to do with any specific programming
language. However, the answer to your question above is "no".
Consider these functions:

int isOneSmallerThanTwo(void) {
if (1<2) return 1; else return 0;
}

int isOneSmallerThanTwo2(void) {
if (1<2) if (1<2) return 1; else return 0; else if (1<2) return 1;
else return 0;
}

Both are of complexity O(1), but still the latter performs one more
comparison than the former.

A more correct statement would be:
"A single atomic operation or a comparison takes O(1) time. A sequence
of such statements takes as much time as its longest operation. A loop
iterating over such statements takes the time of the operations take,
multiplied by the time that iterating over the loop takes."
This statement is not perfect - there may be loopholes.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Show me a good mouser and I'll show you a cat with bad breath."
- Garfield
Nov 14 '05 #4
ju**********@yahoo.co.in (junky_fellow) wrote:
# How do we calculate the complexity of an algorithm?
#
# Am i right if i say the complexity of an algorithm
# is the number of comparisons done in that algorithm?

Peruse a copy of Knuth's Art of Programming. Volume 1 introduces the
topic, and he does space and time complexities throughout. It's too
complex a topic to try to teach in one post.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
I have no respect for people with no shopping agenda.
Nov 14 '05 #5
"junky_fellow" <ju**********@yahoo.co.in> wrote in message

How do we calculate the complexity of an algorithm?

Am i right if i say the complexity of an algorithm
is the number of comparisons done in that algorithm?

This is probably as good a measure as any - count the for, while and ifs in
the code. switch() presents you with a bit of a problem, as do function
pointers.

The problem is that what you really want to know is, psychologically, how
hard is the algorithm to implement? There isn't really a good way of
defining this.
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: sam_cit | last post by:
Hi, I came to read this particular statement with respect to reducing the complexity of a code, The usage of functional maps matrices instead of switch statements should be considered. I...
4
by: raghu | last post by:
How to find the complexity of a C program? Can anyone explain it with an example... Thanks a lot.
26
by: Lionel B | last post by:
Hi, Anyone know if the Standard has anything to say about the time complexity of size() for std::set? I need to access a set's size (/not/ to know if it is empty!) heavily during an algorithm...
15
by: DanielJohnson | last post by:
I am writing a program in which I am removing all the spaces from the string. I thought that I could do it two ways. One was parsing the string character by character and copying onto another...
5
by: Joris van Lier | last post by:
Given two implementations of an algorithm how do I determine the relative computational complexity of each? Are there tools that can determine the relative performance of two algorithms or...
10
by: kinannawaz | last post by:
Can any one help me what will be the time complexity in worst case i.e Big Oh of the following algo I need to resolve it i have worked on it but am not able to come up with the final solution .the...
1
by: amana | last post by:
is there any algorithm that can find time complexity of any program(recursive or non recursive)?
3
by: desaurido | last post by:
I have an algorithm wich I'm trying my best to find it's complexity but I realy have a problem with recursions. I have two versions. I've come down to this: Version1: T(n) = 2n * Sum(1<i<n)...
2
by: orestismaths | last post by:
Can you please explain to me what is the execution time T(n) of the 3 algorithms? Algorithm 1: y ← 0 j ← n while j >= 1 do { k ← 0 while k < n*n do { y ← y +...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.