What are steps to compute algorithm complexity?
-
I have forgotten stuff on how to compute time complexities of algorithms. I am not looking for a Book or 30 page blog to refresh that knowledge. Taking this below algorithm could you please correct in the way i compute the Time Complexity. Thanks Linear Searchbool SeqSearch(int[] arr, int sValue) { for (int index = 0; index < arr.Length-1; index++) if (arr[index] == sValue) return true; return false; } Steps and Logic used Loop through all elements - N Comparison for each index - 1 or is it N Return true or false - 1 Finally I forgot do we add these up or do we multiply them? I assumed we had to add so ended up with N+N+1 so this must be a Big Oh! of N. O(N) Questions Do i multiply the time taken for each steps or add them up For a comparison it is not possible to determine when it will end. So what is time taken ( i assumed 1 because it may be found at first index, N otherwise as last index) Assignments and Return are constant time 1 ? note: Do not refer me to a website. Quora will stay long and people who have the same/similar question will certainly find the answer to this post useful. I can't trust when the other website will be down
-
Answer:
In the example you gave, the "step" we would consider would be "arr[index] == sValue" and it is repeated array length - 1 times. If array length is n, it is in the order of O(n-1) which is same as O(n). Basically, we consider a "step" that is the most time-consuming, recurring in our algorithm and see how many times we are executing that. The step we select for one algorithm might be different from the step we choose for another. That is the reason why we go for asymptotic measures of these steps (when the input size reaches infinity). When we are talking in asymptotic terms, O(n) and O(n+1) are so close that we say they are the same. But they are not same as O(N^2) as regardless of time taken by the steps we considered (n,N), when n,N reach infinity, O(n^2) will be greater than O(N). You will get a very good understanding on how the asymptotic notations work from this "8" (not 30) page pdf :) http://classes.soe.ucsc.edu/cmps102/Spring04/TantaloAsymp.pdf
Naga Krishna Teja Komma at Quora Visit the source
Other answers
You can read about the basic steps: http://www.talentbuddy.co/blog/determining-an-algorithms-complexity-the-basics/
Octav Druta
Related Q & A:
- If Horner’s rule is used what is the number of multiplications to compute P(n) for an polynomial?Best solution by Mathematics
- What is an Ant colony optimization algorithm?Best solution by Stack Overflow
- What's the difference between prim and dijkstra's algorithm?Best solution by Stack Overflow
- What important steps did you take to successfully relocate to another state?Best solution by ChaCha
- What are Steps to Follow a Medical Career?Best solution by study.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.