Comparing Order of Magnitude of Algorithms
Algorithm A:
i = 1
while (i < n):
j = 1
while (j < n):
if ((i+j) == n):
output i, j
j = j + 1
i = i + 1
Algorithm B:
i = 1
while (i < n):
j = n - i
output i, j
i = i+ 1
- For each algorithm, count the number of steps if n = 3.
- Both algorithms perform the same task. What is it?
- What would the expression for number of steps be for any value of n for each algorithm?
- What is the order of magnitude for each algorithm?
- Which algorithm is more efficient in terms of order of magnitude?