0

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.

arunmoezhi
  • 3,082
  • 6
  • 35
  • 54
user655321
  • 143
  • 4
  • 18
  • If you don't like the constants, you can pretty much ignore them, and think of the choices like this: (A) O(n); (B) O(n log n); (C) O(log n); (D) O(n^2). – chiastic-security Oct 02 '14 at 23:17
  • @chiastic-security This makes a lot of sense. For choice B, it has log n twice but you only put one. Why is that? Same question with choice D, with the second n?Thanks – user655321 Oct 02 '14 at 23:23
  • With (B), it's something times n log n, plus some smaller stuff we don't care about. (As n grows, the log n term will be insignificant compared with the n log n term.) Similarly with (D): as n grows, the n term will be insignificant compared with the n^2 term. – chiastic-security Oct 02 '14 at 23:25
  • The constants B, C and D are the time costs for which 3 types of operation? – j_random_hacker Oct 02 '14 at 23:40
  • @j_random_hacker They're unknown constants. Read (D) as: for an array of length n, the algorithm takes some unknown constant times n^2, plus some unknown constant times n, plus some unknown constant. – chiastic-security Oct 03 '14 at 12:34
  • @chiastic-security: I realise that, and I realise that it's possible to obtain the right answer through detective work provided that no 2 of the 4 possibilities are the same as each other except for renamings from one constant to another, but it's easier if we know which constant represents which operation (e.g. is B the time for an array lookup?) Suppose there was a 5th answer, "E) C * n^2 + B * N + D". This is indistinguishable from answer (D), unless we know what B and C represent. – j_random_hacker Oct 03 '14 at 12:59

1 Answers1

0

This looks to me as though it's O(n^2) in the worst case, which is option (D).

Your outer loop runs across the length of the array, so your outer loop goes through n times. (It's actually n-1, but you can ignore that in this type of calculation.)

Then (step 5) you have an inner loop. (I'm assuming this loop ends after step 7 or 8, though it's not quite clear.) This inner loop also runs for up to n iterations.

In total, then, you've got an O(n) loop running inside another O(n) loop; or roughly n iterations multiplied by n iterations. That gives you O(n^2). The constant factors aren't important: it'll take some number times n^2 operations, plus some other smaller stuff we don't care about. That is what option (D) says.

It's not clear what step 3 is doing, by the way. This might be commented out, or it might be an indication that an insertion into the array is happening here. It doesn't matter, though: however the insertion is happening, it can't take more than O(n) steps. It's outside the inner loop, so the outer loop still has O(n) iterations, and the stuff inside it still runs in O(n) time. The total is still O(n^2).

chiastic-security
  • 20,430
  • 4
  • 39
  • 67