Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
1. How does one calculate the run time complexity of an algorithm? ()lease explain with a simple example.
2. What is meaning of size of input in algorithm?
3. What is bestcase,worsecase,averagecase of an algorithm?
4. What is purpose of big-o notation?

Thanks in advance!
Posted
Updated 19-Jan-12 4:31am
v3
Comments
Richard MacCutchan 19-Jan-12 9:35am    
Your question has nothing to do with Data Structures.
Stefan_Lang 19-Jan-12 10:03am    
Your Question has nothing to do with C++ either.

Nor is there a 'Quick answer' to your questions.

Wikipedia should be back online, check it out.
OriginalGriff 19-Jan-12 10:16am    
Reason for my vote of one: too lazy to do basic research.
Manfred Rudolf Bihy 19-Jan-12 10:32am    
Fixed the question title and the tags and also some formatting.

A very simple google lead straight to Wiki: http://en.wikipedia.org/wiki/Analysis_of_algorithms[^] which contains all of the info you need.

In future, please try to do at least basic research yourself, and not waste our time and yours.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 19-Jan-12 10:33am    
Nice link! 5+
Along with the link to the article on Computational Complexity Theory OP should be all ready and set to go. ;)
You really want to read this Wikipedia article: Computational complexity theory[^].

The meaning of input size depends on the concrete algorithm that is being analysed.

For the definition of "bestcase,worsecase,averagecase" please see the referenced article.

Short example:
Appending a node at the end of a list of length n.
Space complexity: n (We need space for n + 1 elements, the constant 1 is dropped in big O notation
Time complexity: n (If we don't have a pointer to the end of the list we have to traverse the whole list thus n)
Time complexity: 1 (If we do have a pointer to the end of the list (like a queue for instance) we can go there directly and need constant time denoted by 1)

Example 2: Worst case
Inserting values into an unbalanced binary tree can cause a degenerated tree (actually a linked list) when the values inserted were already sorted.
The time complexity goes from O(log2n) to O(n) in the worst case.

The purpose of the big O notation is to categorize the computational complexity of algorithms to make them comparable performance wise. Sometimes algorithms can be tuned by trading on complexity off for another. Lets say the runtime of an algorithm with constant space is O(n2) runtime wise it is sometimes possible to get a sub exponential runtime by increasing the space complexity of the algorithm.

Hope I could shed some light onto this for you.

Regards,

Manfred
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 19-Jan-12 17:57pm    
If there is any sense in answering a question by a lazy member at all, it should be an answer exactly as this one. Very good, a 5.
--SA
Manfred Rudolf Bihy 19-Jan-12 18:40pm    
Thanks SA! I know from my own studies that this is a subject that may be hard to grasp at first sight. I do hope that OP will take Griff's and my advice and will read up on this.
:)

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