Chapter 1: Introduction and Algorithm Analysis
Multiple Choice · Basics
Question 1: A Program That Never Ends
Which algorithm criterion does a program fail if it keeps running until it is shut down, like an operating system?
C: An algorithm must stop after finitely many steps on every input. An operating system's instructions can be definite and feasible, and it produces output; it is a computational procedure, not an algorithm, only because it never ends.
Multiple Choice · Basics
Question 2: Opaque Type
main.c includes only fraction.h; the header has typedef struct fraction Fraction;, while the struct fields are defined in fraction.c. main.c: which line does not compile?
B: main.c sees struct fraction as an incomplete type: you can hold a pointer to it and pass it to functions, but you cannot access its fields or apply sizeof to it. Hiding the fields is the C counterpart of an ADT: access is only through interface functions. In option C, fraction_delete(NULL) ends up calling free(NULL), which is harmless.
Spot the Bug · Basics
Question 3: Fraction Addition and a Leak
With the fraction implementation from the slides, what does this fragment print? What is wrong with it memory-wise, and how is it fixed?
Fraction *a = fraction_create(1, 4);
Fraction *b = fraction_create(1, 6);
Fraction *t = fraction_add(a, b);
fraction_print(t);
printf("%d\n",
fraction_equal(t, fraction_create(5, 12)));
fraction_delete(a); fraction_delete(b);
fraction_delete(t);
Show answer
Output: 10/24 and 1.
fraction_adddoes not reduce: num 1·6 + 1·4 = 10, den 4·6 = 24.fraction_equalcross-multiplies: 10·12 = 120 = 5·24 → 1 (equal).fraction_create(5, 12)returns a fraction that is kept in no variable and is neverfreed: memory leak. A counting wrapper shows 4malloccalls and 3freecalls.
Fraction *u = fraction_create(5, 12);
if (t && u)
printf("%d\n", fraction_equal(t, u));
fraction_delete(u);Every create/add result belongs to the caller; releasing it is also the caller's job.
Multiple Choice · Selection Sort
Question 4: After Two Passes
Selection sort (sort) is applied to the array {5, 1, 4, 2, 3}. What does the array look like when the first two passes of the outer loop (i = 0 and i = 1) are done?
D: At i = 0 the smallest value 1 (index 1) is found and swapped with 5: {1, 5, 4, 2, 3}. At i = 1 the smallest of the rest, 2 (index 3), is swapped with list[1] = 5: {1, 2, 4, 5, 3}. A swap exchanges only two cells; the 4 in between stays put, and A mistakes this for a shift.
Output Question · Selection Sort
Question 5: A Macro Argument with a Side Effect
With the three-argument SWAP macro from the slides, what does this fragment print? Did the swap happen?
#define SWAP(x, y, t) \ ((t) = (x), (x) = (y), (y) = (t)) int a[] = {7, 8, 9}, i = 0, j = 2, t; SWAP(a[i++], a[j], t); printf("%d %d %d i=%d\n", a[0], a[1], a[2], i);
Show answer
Output: 7 9 7 i=2.
The preprocessor substitutes the text: ((t) = (a[i++]), (a[i++]) = (a[j]), (a[j]) = (t)). i++ is evaluated twice:
t = a[0]→ 7, i = 1.a[1] = a[2]→ 9, i = 2.a[2] = t→ 7.
Since the comma operator sequences every step, the result is defined, but the swap hit the wrong cells: 8 was lost. A side-effect argument is never given to a macro; write swap(&a[i], &a[j]); i++; instead.
Step-by-Step Trace · Selection Sort
Question 6: Tracing Selection Sort
sort({4, 2, 5, 1}, 4): what is the array after each pass? How many comparisons and how many real swaps (i ≠ min) are made in total?
Start. The outer loop runs n − 1 = 3 passes.
i = 0: 3 comparisons for j = 1, 2, 3. 2 < 4 → min = 1; 1 < 2 → min = 3.
list[0] ↔ list[3]: 1 comes to the front.
i = 1: 2 comparisons (5 and 4 are not smaller than 2). min stays i; SWAP exchanges the cell with itself, which does not count as a real swap.
i = 2: one comparison, 4 < 5 → min = 3.
list[2] ↔ list[3].
i = 3 = n − 1 → the loop ends. Comparisons 3 + 2 + 1 = 6 = n(n−1)/2; swaps 2 (at most n − 1).
void sort(int list[], int n) { int i, j, min, temp; for (i = 0; i < n - 1; i++) { min = i; for (j = i + 1; j < n; j++) if (list[j] < list[min]) min = j; SWAP(list[i], list[min], temp); } }
Multiple Choice · Performance Analysis
Question 7: Step Count
By the rule on the slides (an assignment, each for test, the body and return each count as one step; declarations count 0), how many program steps does the function below take for n = 7?
float avg(float a[], int n) { float s = 0; int i; for (i = 0; i < n; i += 2) s += a[i]; return s; }
B: for i = 0, 2, 4, 6 the body runs 4 times, and the for test runs 5 times (the last test, at i = 8, is false). 1 (s = 0) + 5 + 4 + 1 (return) = 11. In general 2⌈n/2⌉ + 3 steps: still O(n). 17 = 2n + 3 overlooks that the loop advances by two.
Multiple Choice · Performance Analysis
Question 8: Stack Depth of rsum
rsum(list, 1000) is called: at the deepest moment, how many rsum frames are on the stack at once?
A: rsum(list, 1000) → rsum(list, 999) → … → rsum(list, 0): n + 1 = 1001 calls are nested, and none has returned while rsum(list, 0) runs. So Srsum(n) grows linearly with n, while the loop-based sum uses a single frame, O(1) extra memory.
Classic Question · Performance Analysis
Question 9: A Doubling Counter
In the fragment below, how many times is x++ executed for n = 16 and for n = 20? What is the time complexity of the fragment?
x = 0; for (i = 1; i <= n; i *= 2) for (j = 0; j < i; j++) x++;
Show answer
The outer loop takes i = 1, 2, 4, 8, 16 (in both cases 32 > n). The inner loop runs i times:
1 + 2 + 4 + 8 + 16 = 31 (for n = 16 and for n = 20).
In general, with k = ⌊lg n⌋ the total is 2k+1 − 1 < 2n. Despite the two nested loops, the fragment is O(n); the outer loop running lg n times does not make it O(n log n), because the inner loop lengths form a geometric series.
Multiple Choice · Asymptotic Notation
Question 10: The False Statement
Which of the following statements is false?
D: n²/(n·lg n) = n/lg n grows without bound; no c makes n² ≤ c·n·lg n hold for every large n. A is true but uninformative (the tight bound is Θ(n)); B is the comparison count of selection sort; in C, 10n·lg n ≥ n for n ≥ 2.
Classic Question · Asymptotic Notation
Question 11: Finding c and n₀
Given f(n) = 2n² + 9n + 4. If c = 3 is chosen, from which n₀ on does f(n) ≤ c·n² always hold? Also show that f(n) = Θ(n²).
Show answer
2n² + 9n + 4 ≤ 3n² ⇔ n² − 9n − 4 ≥ 0. Since the root (9 + √97)/2 ≈ 9.42, the smallest integer is n₀ = 10:
| n | 9 | 10 | 11 |
|---|---|---|---|
| f(n) | 247 | 294 | 345 |
| 3n² | 243 | 300 | 363 |
Lower bound: for every n ≥ 1, f(n) ≥ 2n² → Ω(n²), c₁ = 2. Together: 2n² ≤ f(n) ≤ 3n² (n ≥ 10) → Θ(n²). A larger c gives a smaller n₀: c = 15 needs only n₀ = 1.
Multiple Choice · Asymptotic Notation
Question 12: Exponential Growth
An algorithm whose step count is proportional to 2ⁿ takes 1 second on a machine for n = 30. About how long does it take for n = 40 on the same machine?
C: 2⁴⁰ / 2³⁰ = 2¹⁰ = 1024 times → 1024 s ≈ 17 min. A assumes linear growth (40/30), B cubic ((40/30)³); D is 2²⁰ s. Adding 10 to n multiplies the time by a thousand: a faster machine cannot close that gap.
Spot the Bug · Performance Measurement
Question 13: Where the Sentinel Goes
The sentinel version of seqsearch from the slides is called as shown below. How many comparisons (list[i] != searchnum) are made when searching for 9? What happens when searching for 7? What is the bug in the code?
int seqsearch(int list[], int searchnum, int n) { int i; list[n] = searchnum; /* sentinel */ for (i = 0; list[i] != searchnum; i++) ; return (i < n) ? i : -1; } /* main: */ int a[5] = {4, 8, 2, 9, 5}; seqsearch(a, 9, 5); seqsearch(a, 7, 5);
Show answer
- For 9: 4 ≠ 9, 8 ≠ 9, 2 ≠ 9, 9 = 9 → 4 comparisons, returns 3.
- For 7: the sentinel
list[5] = 7is written; 5 cells + sentinel = 6 comparisons (n + 1), i = 5 → −1.
Bounds error: a has only 5 elements; list[n] = searchnum writes beyond the end of the array, into a[5]. This is undefined behavior: a neighboring variable may be corrupted. A sentinel search needs n + 1 slots in the array: int a[6] = {4, 8, 2, 9, 5}; (the slides use MAX_SIZE = 5001).
Step-by-Step Trace · Magic Square
Question 14: Magic Square n = 5
magic(5, sq) call, which cells receive the numbers 1–8? What is the first row once the square is full?
Middle of the first row: j = (5 − 1)/2 = 2 → sq[0][2] = 1.
Upper-left diagonal (−1, 1): the row overflowed → r = 4. (4, 1) is empty → 2 goes there.
(3, 0) is empty → 3.
The column overflowed → c = 4. (2, 4) is empty → 4.
(1, 3) is empty → 5.
Upper-left diagonal (0, 2) is taken (it holds 1) → move one down: i = 2, j unchanged → (2, 3) = 6.
(1, 2) is empty → 7.
(0, 1) is empty → 8.
The loop ends at s = 25. First row 15 8 1 24 17; every row, column and diagonal sums to 5·26/2 = 65. 25 placements → O(n²).
void magic(int n, int sq[][MAX]) { int i = 0, j = (n - 1) / 2, s, r, c; sq[i][j] = 1; /* sq is zeroed */ for (s = 2; s <= n * n; s++) { r = (i - 1 < 0) ? n - 1 : i - 1; c = (j - 1 < 0) ? n - 1 : j - 1; if (sq[r][c]) i = (i + 1) % n; else { i = r; j = c; } sq[i][j] = s; } }
Multiple Choice · Performance Measurement
Question 15: A Time of Zero
sort takes about 0.4 seconds and CLOCKS_PER_SEC is 1000. What does the measurement fragment print?
clock_t start = clock(); sort(list, 2000); clock_t stop = clock(); printf("%f\n", (double)((stop - start) / CLOCKS_PER_SEC));
B: clock_t is an integer type; (stop - start) / CLOCKS_PER_SEC first becomes integer division, 400 / 1000 = 0, and the conversion happens afterwards. The correct form is (double)(stop - start) / CLOCKS_PER_SEC. For short jobs, the measurement should still be taken as an average over many repetitions.
Coding Question · Homework
Question 16: Fast Exponentiation
Write the function long power(long x, int n) that computes xⁿ (n ≥ 0) recursively, halving n on every call. power(2, 10): how many calls does it make? What is the complexity?
Show answer
long power(long x, int n) { long y; if (n == 0) return 1; /* base case */ y = power(x, n / 2); /* one call */ return (n % 2) ? y * y * x : y * y; }
power(2, 10) → power(2, 5) → power(2, 2) → power(2, 1) → power(2, 0): 5 calls, result 1024.
n halves on every call: ⌊lg n⌋ + 2 calls → time and stack O(log n). power(x, n/2) * power(x, n/2) written instead would solve the same subproblem twice; the call count would grow to O(n).
Multiple Choice · Homework
Question 17: Number of Calls of binom
With the binom function, how many times is binom(5, 2) called in total (including the first call)?
long binom(int n, int m) { if (m == 0 || m == n) return 1; return binom(n - 1, m) + binom(n - 1, m - 1); }
A: Every leaf (base case) returns 1; since the result is 10, there are 10 leaves. Each internal node makes exactly two calls, so there are 9 internal nodes: 10 + 9 = 19 = 2·C(5, 2) − 1. The same subproblems (e.g. binom(3, 1)) are solved again and again.
Step-by-Step Trace · Homework
Question 18: Call Tree of fib(4)
With fib from the homework answers, calling fib(4) makes how many calls in total, and how many fib frames deep does the stack get at most?
n = 4 > 1: fib(3) will be computed first (assuming the left operand is called first).
fib(3) is not a base case either → fib(2).
fib(2) → fib(1).
Base case: returns 1. The stack is deepest now: 4 fib frames.
fib(0) → 0; fib(2) = 1 + 0 = 1 returns.
The second call of fib(3) is fib(1) = 1; fib(3) = 1 + 1 = 2.
fib(4) moves on to its right operand: fib(2) is recomputed.
fib(1) = 1, fib(0) = 0 → fib(2) = 1.
fib(4) = 2 + 1 = 3. 9 calls in total, at most 4 frames. C does not specify the order in which the operands of + are called; the count and the result do not change.
long fib(long n) { if (n <= 1) return n; return fib(n - 1) + fib(n - 2); } int main(void) { printf("%ld\n", fib(4)); return 0; }