Sum <- 0 // 1 Operation
for i <- 1 to n do // 2n operations
for j <-1 to n do // 2(n-1) operations
k <-1 // 1 operation
while k < n do // n-1 operations
k <- k *c // 2 operations
sum <- sum +1 // 2 operations
In total the number of operations in the code are :
1+2n+2(n-1)+1+(n-1)+2+2 == 5n+3 total # of operations ,
is this how you calculate it , because i understand the concept of each stmt has 3 portions to it ( Comparison, Assignment, Incrementation )
please feel free to correct me is my observations are incorrect
No, that’s probably incorrect.
First of all: You’re thinking too hard. At least for the purposes of Big-O calculations, you can probably treat each assignment as a single operation, no matter whether it’s a constant assignment or a calculated value.
Second of all: You aren’t thinking hard enough. The fourth line is a single operation, but it’s run
n * ntimes, so it should be counted asn^2, not1. Similarly for other lines in loops.