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 finding the largest value in an unsorted array of n numbers?

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

Which complexity class grows fastest for large n?

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

Which of these grows slowest as n becomes large?

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

How many levels does the recursion of merge sort have for an array of n elements?

  1. AAbout n
  2. BAbout √n
  3. CAbout log₂ n
  4. DAbout n / 2
5.Medium

Which sorting algorithm is stable?

  1. AMerge sort
  2. BQuicksort (typical in-place version)
  3. CHeap sort
  4. DSelection sort (typical version)
6.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 average cost per operation over a worst-case sequence of operations
  4. DThe cost of an algorithm on the best input
7.Hard

The halting problem shows that:

  1. AEvery program eventually halts
  2. BNo algorithm can decide for every program and input whether it halts
  3. CSome programs run faster on faster computers, so halting depends on hardware
  4. DLoops should be avoided
8.Hard

Which technique gives an expected O(n log n) running time for quicksort on any input?

  1. AChoosing the pivot uniformly at random
  2. BAlways choosing the first element
  3. CUsing recursion only on the left side
  4. DSorting the input before running quicksort

Answer key

  1. 1. B · O(n)
  2. 2. C · O(2ⁿ)
  3. 3. C · O(log n)
  4. 4. C · About log₂ n
  5. 5. A · Merge sort
  6. 6. C · The average cost per operation over a worst-case sequence of operations
  7. 7. B · No algorithm can decide for every program and input whether it halts
  8. 8. A · Choosing the pivot uniformly at random

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