Design and Analysis of Algorithms · Unit 2 · 4 hrs
Iterative Algorithms
Exam-focused notes for Iterative Algorithms (Design and Analysis of Algorithms, CSC325): what the TU syllabus asks and how it has actually been tested, with 7 solved past questions from this unit.
Iterative Algorithms in short
This unit works through non-recursive algorithms in three families: two simple numeric routines with their own recurrences, linear search, and the three quadratic sorting algorithms that form the baseline every faster sort is measured against.
What this unit covers
- Basic Algorithms: Algorithm for GCD, Fibonacci Number and analysis of their time and space complexity
- Searching Algorithms: Sequential Search and its analysis
- Sorting Algorithms: Bubble, Selection, and Insertion Sort and their Analysis
Sorting Algorithms
2080 · 5 marks
Write down algorithm of insertion sort and analyze its time and space complexity. [5]
2078 · 5 marks
Write the algorithm for bubble sort and explain its time complexity. [5]
2076 · 5 marks
Write the algorithm for selection sort and explain its time and space complexity. [5]
Show solved answerHide answer
Selection Sort: Algorithm and Complexity Analysis
Definition
Selection Sort is a simple comparison-based sorting algorithm that works by repeatedly selecting the minimum element from the unsorted portion of the array and placing it at the correct position.
Algorithm (Steps)
- Start.
- Let array A have n elements (index 0 to n-1).
- For each position i from 0 to n-2:
- Assume the current position i holds the minimum element (set
min_index = i). - Scan the remaining unsorted portion (j from i+1 to n-1).
- If
A[j] < A[min_index], updatemin_index = j. - After scanning, swap
A[i]withA[min_index].
- Assume the current position i holds the minimum element (set
- Repeat step 3 until the array is fully sorted.
- Stop.
Pseudocode
SelectionSort(A, n)
for i = 0 to n-2
min_index = i
for j = i+1 to n-1
if A[j] < A[min_index]
min_index = j
swap(A[i], A[min_index])
Example Trace
Sort: A = {64, 25, 12, 22, 11}
| Pass | Array State | Action |
|---|---|---|
| 1 | 11 25 12 22 64 | Min=11, swap with index 0 |
| 2 | 11 12 25 22 64 | Min=12, swap with index 1 |
| 3 | 11 12 22 25 64 | Min=22, swap with index 2 |
| 4 | 11 12 22 25 64 | Min=25, already in place |
| Done | 11 12 22 25 64 | Sorted |
Time Complexity Analysis
Using the RAM model (each comparison and memory reference counts as 1 step):
- The outer loop runs (n - 1) times.
- For each pass i, the inner loop runs (n - 1 - i) times.
- Total comparisons:
$$T(n) = (n-1) + (n-2) + (n-3) + \ldots + 1 = \frac{n(n-1)}{2}$$
| Case | Complexity | Reason |
|---|---|---|
| Best Case | O(n²) | Inner loop always runs fully; no early exit |
| Average Case | O(n²) | Same number of comparisons regardless |
| Worst Case | O(n²) | Same number of comparisons regardless |
Unlike Bubble Sort (which has O(n) best case), Selection Sort always performs O(n²) comparisons because it must scan the entire unsorted portion to find the minimum, even if the array is already sorted.
Space Complexity Analysis
- Selection Sort sorts the array in-place.
- Only a constant number of extra variables are used:
i,j,min_index, and a temporary swap variable. - The array A itself takes n memory references, but no additional array is needed.
$$\text{Space Complexity} = O(1)$$
This is consistent with the approach used in Bubble Sort (O(1) extra space) as noted in the course notes, since both are in-place sorting algorithms.
Summary Table
| Property | Value |
|---|---|
| Best Time Complexity | O(n²) |
| Average Time Complexity | O(n²) |
| Worst Time Complexity | O(n²) |
| Space Complexity | O(1) |
| Sorting Type | In-place, Not stable |
Searching Algorithms
2082 · 5 marks
State the time and space complexity for sequential search. Write the rules for master theorem for finding asymptotic bounds. [5]
Show solved answerHide answer
Sequential Search: Time & Space Complexity + Master Theorem Rules
Part 1: Time and Space Complexity of Sequential Search
Sequential search (also called linear search) compares the search element with each element in the list one by one from the beginning.
Time Complexity
| Case | Condition | Complexity |
|---|---|---|
| Best Case | Element found at the first position | O(1) |
| Average Case | Element found somewhere in the middle | O(n) |
| Worst Case | Element found at last position or not found at all | O(n) |
Since the loop executes at most n times, the overall time complexity is O(n).
Space Complexity
- Sequential search only requires a fixed number of variables (search element, loop counter, etc.)
- No extra data structure is used
- Therefore, space complexity = O(1) (constant space)
Part 2: Master Theorem Rules for Asymptotic Bounds
The Master Theorem is used to find the time complexity of divide-and-conquer recursive algorithms whose recurrence relation is of the form:
$$T(n) = aT\left(\frac{n}{b}\right) + f(n)$$
Where:
a >= 1: number of subproblemsb > 1: factor by which input size is reducedf(n): cost of work done outside the recursive calls
Let p = log_b(a)
Rule 1: f(n) is polynomially smaller than n^p
If:
$$f(n) = O(n^{p - \epsilon}) \quad \text{for some } \epsilon > 0$$
Then:
$$T(n) = \Theta(n^p) = \Theta(n^{\log_b a})$$
The recursive work dominates.
Rule 2: f(n) is asymptotically equal to n^p
If:
$$f(n) = \Theta(n^p) = \Theta(n^{\log_b a})$$
Then:
$$T(n) = \Theta(n^p \log n) = \Theta(n^{\log_b a} \cdot \log n)$$
Both the recursive work and the combining work are equal, so a log factor is added.
Rule 3: f(n) is polynomially larger than n^p
If:
$$f(n) = \Omega(n^{p + \epsilon}) \quad \text{for some } \epsilon > 0$$
AND the regularity condition holds:
$$a \cdot f\left(\frac{n}{b}\right) \leq c \cdot f(n) \quad \text{for some } c < 1$$
Then:
$$T(n) = \Theta(f(n))$$
The combining work dominates.
Summary Table
| Condition | Result |
|---|---|
| f(n) < n^(log_b a) polynomially | T(n) = Theta(n^(log_b a)) |
| f(n) = Theta(n^(log_b a)) | T(n) = Theta(n^(log_b a) * log n) |
| f(n) > n^(log_b a) polynomially | T(n) = Theta(f(n)) |
Basic Algorithms
2081 · 5 marks
Write an algorithm to find the $n^{th}$ fibonacci number with its time and space complexity. [5]
2079 · 5 marks
Explain the iterative algorithm to find the GCD of given two numbers and analyze its complexity. [5]
Make Unit 2 stick
Practice CSC325 with flashcards & quizzes