I am attempting to answer some questions that I've come across. I have been watching some videos regarding running time of algorithms. From what I understood, you have to count each operation in an iteration to get the running time.
I have the following question which I don't quite understand. The constants in choice are throwing me off. Can anyone try to explain?
What is the running time of the following code? (A is an array size N, "B", "C", "D" are constants in choices)
1. for j ← 2 to length(A)
2. key ← A[ j ]
3. //Insert A[ j ] is added in the sorted sequence A[1,..j-1]
4. i ← j - 1
5. while i >= 0 and A [ i ] > key
6. A[ i +1 ] ← A[ i ]
7. i ← i -1
8. A [i +1] ← key
The answer options are:
A)B * n + C + D
B)B * n * log n + C * log n + D
C)B * log n + C + D
D) B * n^2 + C * n + D
What matters to me is not really the answer, but how to actually get to it. Thanks.