I was going through my Data Structures and Algorithms notes, and came across the following examples regarding Time Complexity and Big-O Notation:
The columns on the left count the number of operations carried out in each line. I didn't understand why almost all the lines in the first example have a multiple of 2 in front of them, whereas the other two examples don't. Obviously this doesn't affect the resulting O(n), but I would still like to know where the 2 came form.

- 1
- 1

- 1,433
- 3
- 18
- 36
-
That looks really strange. Without any other slides/pictures it doesn't make much sense for me. Maybe there is something missing in these pictures. (There is also no Input + Output description in the first one). The only possible explanation, which is not compatible with the slides though, would be the case, that algorithm one is computing max and min together. – sascha May 30 '16 at 20:19
-
maybe they realized half way through that they were supposed to be talking about big-O; otherwise I see no reason for the 2 – May 30 '16 at 20:20
-
Even if they would prune some O-notation stuff, it doesn't make any sense for me, that a variable-incrementation (inc counter) is more than *1 steps*. – sascha May 30 '16 at 20:22
-
Somewhere a definition of _operation_ should be provided. So knowing what is count as operation we can rationale numbers – Lol4t0 May 30 '16 at 20:28
1 Answers
I can only find one explanation for this: the sloppiness of the author of the slides.
In a proper analysis one have to explain what kind of operations are performed at which time for what input (like for example this book on page 21). Without this you can not even be sure whether we count multiplication of 2 numbers as 1 operation or 2 or something else?
These slides are inconsistent. For example:
In slide1 currentMax = A[0]
takes 2 operations. Kind of makes sense if you take finding 0-th element in array as 1 operation and assigning as another one. But in the slide3 n
iterations of s = s + X[i]
takes n operations. Which means that s = s + X[i]
takes 1 operation. Also kind of makes sense we just increase one counter.
But it is totally inconsistent with each other, because it doesn't makes sense that a = X[0]
is 2 operations, and a = a + X[0]
where you do more takes only 1.

- 214,103
- 147
- 703
- 753