CMPU-203: Introduction to Computer Science III

Quiz #10
Due: Monday, Dec. 3

  1. Explain why quicksort has worst-case n2), but typically runs in n log(n).

  2. For each of the following sections of code, identify its worst-case complexity class as constant, log n, linear, n log n, or n2

    1. for (int i =0; i < n; i++)
        for (int j=0; j < n; j++)
          if (a[i] < a[j]) swap(a,i,j);
      

    2. for (int i=0; i < n; i++)
        for (int j=i; j < n; j++)
          if (a[i] < a[j]) swap(a,i,j);
      

    3. for (int i=0; i < 10; i++) swap(a,i,1);
      

    4. for (int step=n/2, int i=step; i; step/=2, i+=step)
        if (a[i] < key) step = abs(step);
        else if (a[i] > key) step = abs(step) * -1;
        else break;
      

    5. for (int i=0; i < n; i++)
        for (int j=n; j; j/=2)
          a[i,j] = mark;