Function comparison

These are the multiple choice questions. Please, try to understand why the answer is what it is. If you are stuck, ask in the comments.

  1. Which of the following are not O(n2). Answer: n3/sqrt(n)
    1. (1510) * n + 12099
    2. n1.98
    3. n3/sqrt(n)
    4. 220 * n
  2. Sort the functions by increasing order of complexity. Answer: f3, f2, f4, f1
    1. f1(n) = 2n
    2. f2(n) = n3/2
    3. f3(n) = n log n
    4. f4(n) = nlog n
  3. Which of the following loops is the fastest? Assume the same code inside the loops. Also assume n is a positive integer. Answer: for (idx = 1; idx < n; idx *= 2)
    1. for (idx = 0; idx < n; ++idx)
    2. for (idx = 0; idx < n; idx += 2)
    3. for (idx = 1; idx < n; idx *= 2)
    4. for (idx = n; idx > -1; idx /= 2)

Leave a Reply

Your email address will not be published. Required fields are marked *