Algorithms & complexity worksheet with answers

Free printable Algorithms & complexity worksheet: 8 problems from easy to exam level, with an answer key. Print it or save it as a PDF.

For teachers: free to print, copy and use in class. Every new worksheet has new values and its own answer key. A link to ExamProblems on your course page is appreciated.

Name: ____________________   Date: ____________

1.Easy

What is the time complexity of linear search in an unsorted array of n elements (worst case)?

  1. AO(n)
  2. BO(1)
  3. CO(log n)
  4. DO(n²)
2.Easy

Which sorting algorithm repeatedly swaps adjacent elements that are in the wrong order?

  1. ABubble sort
  2. BMerge sort
  3. CHeap sort
  4. DQuicksort
3.Easy

What is the time complexity of finding the largest value in an unsorted array of n numbers?

  1. AO(log n)
  2. BO(n log n)
  3. CO(n)
  4. DO(1)
4.Medium

What does amortised analysis measure?

  1. AThe expected cost over random inputs
  2. BThe cost of the single most expensive operation anywhere in the sequence
  3. CThe cost of an algorithm on the best input
  4. DThe average cost per operation over a worst-case sequence of operations
5.Medium

A recurrence T(n) = 2T(n/2) + O(n) solves to:

  1. AO(n²)
  2. BO(n)
  3. CO(log n)
  4. DO(n log n)
6.Medium

About how many comparisons does binary search need in the worst case on a sorted array of 1,000,000 elements?

  1. AAbout 20
  2. BAbout 500,000
  3. CAbout 100
  4. DAbout 1,000
7.Hard

Which statement about little-o notation is correct?

  1. An² = o(n)
  2. Bn = o(n²), but n is not o(n)
  3. C2n = o(n), because 2n grows slowly
  4. Dn = o(n), since every function is little-o of itself
8.Hard

Strassen's algorithm multiplies matrices using 7 instead of 8 recursive multiplications of half-size blocks. What is its running time?

  1. AΘ(n2)
  2. BΘ(n² log n)
  3. CΘ(n³)
  4. DΘ(nlog₂ 7), about n2.81

Answer key

  1. 1. A · O(n)
  2. 2. A · Bubble sort
  3. 3. C · O(n)
  4. 4. D · The average cost per operation over a worst-case sequence of operations
  5. 5. D · O(n log n)
  6. 6. A · About 20
  7. 7. B · n = o(n²), but n is not o(n)
  8. 8. D · Θ(nlog₂ 7), about n2.81

Full step-by-step solutions: practise this topic on examproblems.com.