I'm studying for a computing exam and came past the following question on a past paper and need help with it.
When would algorithm A be slower than algorithm B? Demonstrate your answer with the help of an example. Also what will the value of SUM be at the end of each algorithm if the size is set to $10'000$?
Algorithm A:
SET sum TO 0
FOR i=1 to size
FOR j=1 to 10000
sum=sum+1
Algorithm B:
SET sum TO 0
FOR i=1 to size
FOR j=1 to size
sum=sum + 1
I came up with this answer but not sure if it is correct:
The algorithm A will be slower than algorithm B when the performance of the algorithm is directly proportional to the cubed or more of the size of the input data set, for example if the Big O notation becomes $O(N^3)$ or $O(N^4)$ or $O(N^5)$ etc. The Big $O$ notation $O(N^3)$ nesting the for loops in two more for loops:
Set Sum TO 0
For i=1 to size
For k=1 to size
For l=1 to size
For j=1 to 10000
sum=sum+1
FOR i=1 TO 100 DO SUM+=1
- how much times sum+=1 operation will be performed? $\endgroup$ – Bulat Jul 9 at 23:14