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 Search
bool 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 –
1or is itN - 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: Please Please do not refer me to a website. SO 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 taken down etc. Also i am not bothered of efficiency, time complexity but the process/steps used to find it.
Resource
http://faculty.simpson.edu/lydia.sinapova/www/cmsc250/LN250_Weiss/L03-BigOh.pdf
I just wanted to put this link to pdf that explains clearly how to account for what statements and when. Just like i wanted.
Think in terms of primitive operations (memory accesses and arithmetic/logic operations).
Count them for your algorithm given the size of the input,
N(arr.Lengthin your case).Then see how that total number of operations relates to
N, whether it’s just some constant or a polynomial ofN(e.g.N3) or a logarithm ofNor an exponent ofNor something else.If it turns out something like
N+1 or 2*N, you should neglect the small constant because you’re primarily concerned about what happens whenNis large and about the overall behavior.That’s the basics.
Here’s the approximate time complexity for the worst case (when
sValueisn’t inarr[]):int index = 0;is 1index < arr.Length-1;is 1 (but could be up to, say, 10), repeatedarr.Lengthtimesindex++is 1 (but could be up to, say, 3), repeatedarr.Lengthtimesif (arr[index] == sValue)is 1 (but could be up to, say, 10), repeatedarr.Lengthtimesreturn value;is 1So you have something like 1 + 1 *
arr.Length+ 1 *arr.Length+ 1 *arr.Length= 1 + 3 *arr.Length+ 1. You simplify that down toarr.Length, hence O(N).On average you’ll only have
arr.Length/ 2 iterations. Hence on average you have 1 + 1 *arr.Length/ 2 + 1 *arr.Length/ 2 + 1 *arr.Length/ 2 + 1 = 2 + 1.5 *arr.Length. Again, O(N).But you should really read about this stuff.