Worked Examples: Lecture 01 - Introduction

Step-by-step solutions to build your algorithmic intuition.

Example 1: Insertion Sort Trace
❓ Problem: Sort A = [5, 2, 4, 6, 1, 3] using Insertion Sort.

Key Idea: At each step, insert the current element into its correct position among the already-sorted elements.

Initial: [5, 2, 4, 6, 1, 3]
Pass 1 (key=2): Compare 2 with 5. Shift 5 right, insert 2. → [2, 5, 4, 6, 1, 3]
Pass 2 (key=4): Compare 4 with 5. Shift 5 right, insert 4. → [2, 4, 5, 6, 1, 3]
Pass 3 (key=6): 6 > 5, no shift needed. → [2, 4, 5, 6, 1, 3]
Pass 4 (key=1): Shift all 4 elements right, insert 1 at front. → [1, 2, 4, 5, 6, 3]
Pass 5 (key=3): Shift 4,5,6 right, insert 3 after 2. → [1, 2, 3, 4, 5, 6]
Example 2: Finding Minimum - Pseudocode
❓ Problem: Write pseudocode to find the minimum element in an array A of n elements.
FIND-MIN(A, n):
    min = A[1]
    for i = 2 to n:
        if A[i] < min:
            min = A[i]
    return min

Time Complexity: $O(n)$ — we look at each element exactly once.

Example 3: Loop Invariant for Find-Min
❓ Problem: State and prove a loop invariant for the Find-Min algorithm.

Loop Invariant: At the start of iteration $i$, min holds the minimum value of the subarray $A[1..i-1]$.

Initialization: Before the first iteration ($i=2$), min = A[1], which is trivially the minimum of $A[1..1]$. ✓

Maintenance: If the invariant holds at iteration $i$, then after comparing $A[i]$ with min and updating if necessary, min becomes the minimum of $A[1..i]$. ✓

Termination: When $i = n+1$, the invariant tells us min holds the minimum of $A[1..n]$, which is the entire array. ✓

Example 4: Best and Worst Case for Insertion Sort
❓ Problem: Construct best-case and worst-case inputs for Insertion Sort of size 5.

Best Case: [1, 2, 3, 4, 5] (already sorted)

  • Each element is already in place, so no shifts are needed.
  • Time: $O(n)$ — just n-1 comparisons.

Worst Case: [5, 4, 3, 2, 1] (reverse sorted)

  • Every element must be compared and shifted past all previous elements.
  • Time: $O(n^2)$ — specifically, $1 + 2 + 3 + 4 = 10$ shifts.
Example 5: Counting Comparisons
❓ Problem: How many comparisons does Linear Search make in the worst case on an array of size n?

Answer: $n$ comparisons

Explanation: In the worst case, the target is not in the array (or is at the last position). The algorithm must check every element, making exactly $n$ comparisons.

This gives us a time complexity of $O(n)$ for Linear Search.