CS1010 Notes
  • Welcome
  • Lec/Tut/Lab/Exes
    • Lecture
      • Lec 01 - Computational Problem Solving
      • Lec 02 - Functions and Types
      • Lec 03 - Basic C Programming
      • Lec 04 - Conditionals
      • Lec 05 - Loops
      • Lec 06 - Call Stacks, Arrays
        • Diagnostic Quiz
      • Lec 07 - Pointers, Memory management
        • Diagnostic Quiz
      • Lec 08 - Multi-d Array, Efficiency
        • Diagnostic Quiz
      • Lec 09 - Searching and Sorting
        • Diagnostic Quiz
      • Lec 10 - More Recursion
        • Diagnostic Quiz
      • Lec 11 - Strcut & Standard I/O
        • Diagnostic Quiz
      • Lec 12 - Recap
    • Tutorial
      • Tut 01 - Computational Problem-Solving
      • Tut 02 - Functions and Conditionals
      • Tut 03 - More on Conditionals
      • Tut 04 - Loops
      • Tut 08 - Searching and Sorting
    • Lab
      • Lab 01 - Unix/Vim Setup
      • Lab 02 - Debugging
      • Lab 03 - Assert
      • Lab 04 - Test Cases
      • Lab 05 - Arrays
      • Lab 06 - Memory Errors
      • Lab 07 - Compiling with Clang
      • Lab 08 - C Preprocessor
      • Lab 09 - Backtracking
      • Lab 10 - Struct and Wrap up
    • Exercises
      • Exercise 3 - Fixed-Length Arrays
      • Exercise 4 - Dynamic Arrays and Strings
      • Exercise 6 - Searching and Sorting
      • Exercise 7 - More Recursion
      • Exercise 8 - Struct
  • Past Year Exam
    • Midterm PE
      • PE1 (AY18/19)
      • PE1 (AY20/21)
      • PE1 (AY21/22)
      • PE0 (AY22/23)
      • PE0 (AY23/24)
    • Midterm Paper
      • Midterm (AY18/19)
      • Midterm (AY20/21)
      • Midterm (AY21/22)
      • Midterm (AY22/23)
    • PE1 Review
      • PE1 (AY23/24)
    • PE2 Review
      • PE2 (AY18/19)
      • PE2 (AY20/21)
      • PE2 (AY21/22)
      • PE2 (AY22/23)
      • PE2 (AY23/24)
    • Final Paper
      • Final (AY18/19)
      • Final (AY20/21)
      • Final (AY21/22)
      • Final (AY22/23)
      • Final (AY23/24)
  • Current Year Exam
    • PE0 (AY24/25)
    • PE1 (AY24/25)
    • PE2 (AY24/25)
    • Final (AY24/25)
  • Toolbox
    • Vim & Unix
    • GDB
  • After CS1010
Powered by GitBook
On this page
  • 11. Sorting
  • 12. Sorting
  • 16. Recursion
Edit on GitHub
  1. Past Year Exam
  2. Final Paper

Final (AY23/24)

PreviousFinal (AY22/23)NextPE0 (AY24/25)

Last updated 6 months ago

Problems

Question paper without answers:

Question paper with answers:

11. Sorting

Insertion sort is fast when it comes to sorting an almost sorted list.

Here is the table summarise the best-case, worst-case time complexity for different sorting algorithms:

Sorting algorithm
Sorted array (best case)
Inversely sorted array (worst case)
General array (average case)

Bubble Sort

0

Insertion Sort

0

Selection Sort

12. Sorting

Notice that in this question, we are talking about the number of swaps, not the running time. So, for the selection sort we are always making nnn times of swap.

Below is the table summarises the number of swap needed for different sorting algorithms:

Sorting algorithm
Sorted array (best case)
Inversely array (worst case)
General array (average case)

Bubble Sort

0

Insertion Sort

0

Selection Sort

16. Recursion

This kind of sequence (each binary number is one-bit different from its previous) is called grey code! Amazing right!

In this problem, it utilises a hidden information: that is the string is intialized to be all 0! So, it is safe for the first line of our recursion to be just generate(str, n, k + 1) since once it reaches the end, it will print all 0.

Then our idea may be a bit clearer, we modify our current bit str[k] to be different from what we print out previously, and then print it out by calling generate(str, n, k + 1) again.

generate(str, n, k + 1);
str[k] = (str[k] == '0') ? '1' : '0';
generate(str, n, k + 1);

To make our mind clearer, Line 1 is to generate the "previous" binary string, then Line 2 modifies the "next" binary string to be exactly one bit different from the previous and then print it out calling the substring recursively.

O(n2)O(n^2)O(n2)
O(n2)O(n^2)O(n2)
O(n2)O(n^2)O(n2)
O(n2)O(n^2)O(n2)
O(n)O(n)O(n)
O(n)O(n)O(n)
O(n)O(n)O(n)
O(n2)O(n^2)O(n2)
O(n2)O(n^2)O(n2)
O(n2)O(n^2)O(n2)
O(n2)O(n^2)O(n2)
O(n)O(n)O(n)
O(n)O(n)O(n)
O(n)O(n)O(n)
113KB
final-2324-s1.pdf
pdf
Question without Answers
149KB
final-2324-s1-with-comments.pdf
pdf
Question with Answers