C++

if (a = b){// this store value of b in a

must be replaced by:

C++

if (a==b){// this compare a and b for equality

See more:

C++

for (i = 0; i < m, i + +){ for (j = 0; j < n, j + +){ if (a = b){ arr[i][j] = 1; } else{ arr[i][j] = 2; } } } for (i = 0; i < m, i + +){ for (j = 0; j < n, j + +){ arr[i] += arr[i][j]; } } for (i = 0; i < m, i + +){ if (a = b){ p[i] = i q [i] = i } }

C++

for (i = 0; i < m, i + +){ sum = 0; for (j = 0; j < n, j + +){ if (a = b){ arr[i][j] = 1 sum += 1; } else{ arr[i][j] = 2 } } if (c = d){ arr[i] = sum; p[i] = i q [i] = i } }

I tried to make a time complexity equation step by step for both program. and which one is better and how much? Could you please help on that?

for program-1:

m*n*c1 + m*n*c2 + m*c3

for program-2

m*n*c1 + m*c2

Comments

Not your question, but your programs are wrong:

must be replaced by:

C++

if (a = b){// this store value of b in a

must be replaced by:

C++

if (a==b){// this compare a and b for equality

Permalink

Share this answer

Which do you think is faster? Look at the complexities you have calculated, and compare them.

O(1) has a fixed time: no matter how many data samples are used, it will always execute in the same time.

O(N) directly compares the number of data samples to the execution time in a linear fashion: if you increase N from 10 to 20, execution time will double.

O(2N) is also linear, if you increase N from 10 to 20, execution time will also double. The complexity is equivalent, but slower than O(2N)

O(N^{2}) is exponential - a small increase in N results in a big increase in execution time.

Look at your two complexities and it's pretty obvious which is faster if you think about it.

This is your homework, not ours: your tutor is looking for you thoughts, not mine. So give it a try!

O(1) has a fixed time: no matter how many data samples are used, it will always execute in the same time.

O(N) directly compares the number of data samples to the execution time in a linear fashion: if you increase N from 10 to 20, execution time will double.

O(2N) is also linear, if you increase N from 10 to 20, execution time will also double. The complexity is equivalent, but slower than O(2N)

O(N

Look at your two complexities and it's pretty obvious which is faster if you think about it.

This is your homework, not ours: your tutor is looking for you thoughts, not mine. So give it a try!

Permalink

Share this answer

v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject,
20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8
+1 (416) 849-8900

REPOSTThis is now your

THIRDcopy of exactly the same question:How can I calculate time complexity and compare between given two algorithms? - Algorithms Discussion Boards[^]

Calculate time complexity step by step of given two program program - Algorithms Discussion Boards[^]