5101 - Design & Analysis of Algo
← 5101 - Design & Analysis of Algo
session: "5101-design-analysis-of-algo/2026-09-20 0000 DAA 20 Sep"
title: "DAA 20 Sep"
date: "2026-09-20 00:00"
duration: "45m 28s"
language: "en"
model: "deepseek-ai/DeepSeek-V4.1-Flash-1017"
parts: 2
transcript_chars: 28952
generated: "2026-11-23T23:44:46+05:30"
This class began with a clarification of lab/assignment evaluation, then recapped binary search, merge sort and quick sort before going deeper into quick sort: pivot selection, worst-case behaviour, recurrence relations, the median-of-3 pivot strategy, and a quick-sort + insertion-sort hybrid ("quick insertion"). It then explained why quick sort is faster than merge sort in practice, and introduced the maximum subarray problem (motivated by a stock buy/sell story) together with its brute-force solution. The class previewed the three solution variants of maximum subarray (brute force, divide and conquer, dynamic programming) and closed early due to the instructor's exam duty, with divide and conquer announced for the next class.
There are four lab assignments; lab work is for practice, and a separate quiz will be conducted. Lab assessment marks may be shown/added if beneficial — if you do well in the quiz, those marks are not added; if you do not do well, the lab assessment marks are added. This matters because it defines how the course is graded and how lab effort converts into marks.
Merge sort recursively divides a large problem, requires additional space for storing results, and is n log n for best, worst and average cases. Binary search recursively divides and compares with the middle element, is order log n, and requires the array to always be sorted. Quick sort's best/average case complexity is equivalent to merge sort; its worst case reaches n² when the pivot is selected as the extreme element (smallest or largest) in each iteration. These recaps set the baseline against which quick sort's behaviour is analysed.
Identify a pivot and fix its position; elements greater than the pivot go to one side, less than to the other. The scan starts from the left (left = left + 1), goes left to right; if the condition fails, it changes direction toward the left. This continues until left index ≤ right index; if that condition fails, the algorithm terminates. This mechanism is the basis for all the complexity analysis that follows.
Pivot options are: first element, last element, median element, random selection. All of these may lead to the worst case (not guaranteed 100%, but may). This is why a smarter pivot strategy (median of 3) is needed.
A strategy for pivot selection in quick sort that avoids the worst case, reducing computational time requirement by "14 plus" (as stated). Basic idea: take three indexes — low (first index), median index, high (last index) — and arrange them so that first < median < last. Then replace the median with the second-last element, and that acts as the pivot; then run the quick sort operation. It connects to pivot selection: it is the practical fix for the worst case discussed earlier.
For very small arrays, quick sort does not perform as well as insertion sort (no visible reduction in clock time; only a few milliseconds difference), because quick sort still divides, selects a pivot, etc. Variant: when using quick sort recursively, switch to insertion sort for small arrays. The base case (stopping criterion of recursion) is changed: instead of stopping at size ≤ 1, stop when array size ≤ 5 (any threshold can be set) and directly apply insertion sort. Example visualization: array of size 20 → partition into 8 and 12 → further partitions produce sizes like 5, 3, 7; wherever size ≤ 5, immediately call insertion sort; the 7 is further divided into 3 and 4, and insertion sort is called there too. Why this is still better despite insertion sort being n²: we are dealing with only about one-fourth of the size (n/4), so n² is not very big relative to the maximum number of elements; clock time difference is small. Also, when subarrays are fairly sorted, insertion sort tends toward its best case.
Due to external memory requirement (caching requirement) — merge sort needs extra space, quick sort does not. The inner loop consists of an increment or decrement by one, a test, and a jump — this is faster. In merge sort, comparisons involve the entire array and then sorting in order; in quick sort, comparisons are confined around a single element's placement (fixing the pivot's position), with no extra juggling as in merge sort. In the Q&A: the worst case will rarely appear; we are not comparing on the worst case. If we do a worst-case comparison, merge sort is better than quick sort — but we are not assuming someone will provide an already sorted array for sorting.
Motivating story: a share-market-listed company (Volatile Chemical Corporation) whose shares are volatile. Condition: buy one unit of stock only one time and sell it on a later date (not the same day; selling after the closing of the trading day is allowed; intraday stock prices are not applicable here). You are allowed to know future stock prices, but no ML technique is to be applied. Objective: buy at the lowest price and sell at the highest possible price — identify entry and exit dates. With n days, we are trying to do nC2 (choose two days). Reformulated: given an array containing both positive and negative numbers, identify a subarray whose sum is highest — the maximum subarray (maximum sum subarray). If all elements are positive: the maximum subarray is the sum of all elements. If all elements are negative: choose the least number with negative sign (the negative value closest to zero, e.g., between −1 and −20 choose −1), because adding negatives increases negativity. This problem is the bridge to the next topic: it has three solution forms with complexities n², n log n and n.
Form all combinations and pick whichever gives the highest sum. Implementation: one loop for row and one loop for column → complexity order n². Drawback: irrespective of the available solution, all possibilities are explored. This is the first of the three variants previewed.
Brute force: order n². Divide and conquer: order n log n (as stated in the lecture). Dynamic programming: order n — the best approach; drawback is that it requires memory to store previous outcomes. This preview frames the upcoming classes.
Named by the instructor as the well-known algorithm "in this" (dynamic programming); students are told to check it out. No further detail given. The name is as spoken in the transcript and the intended algorithm name is not certain — verify against the lecture material.
Stock price example (dates/prices as spoken):
Brute force maximum subarray example (7 elements, indices 1–7):
Student's example (from Q&A): array with 20 as the highest positive number; if you add surrounding negatives they become more negative, so choose 21 [as spoken]; e.g., 20, −11, 25 — enter on this day and exit on that day for maximum profit; the lower negative number can be on either side, with negative numbers in between.
Q (assignment): Will there be a separate assignment besides labs 1–4, or only the lab assignments count?
A: There will be a separate quiz; lab work is practice. If marks are demanded, the lab assessment can be shown so they can be added. If you do well in the quiz, the lab marks are not added; if you do not do well, the lab marks are added.
Q: If quick sort takes n² in the worst case and merge sort takes n log n, how can we say quick sort is faster?
A: The worst case will rarely appear; we are not comparing on the worst case. In a worst-case comparison, merge sort is better — but we are not assuming someone will provide an already sorted array for sorting.
Q: What about non-numeric data (e.g., strings)? Can we convert to ASCII and then sort to increase performance?
A: Here we are primarily talking about numbers. For alphabetical data, you convert into ASCII code — A, B, C, D have different numbers (around 60s; for smaller letters around 90s, as spoken) — and sort accordingly; 'a' has a lower number and 'z' a higher number. The scheme is applicable; just convert to its equivalent (e.g., binary equivalent) and perform the sorting.
Q (on the array with negative values on both sides): Which median side is taken?
A: It is not about the median; we select the subarray that gives the highest values. In the example, the only possibility is to enter before 13 and exit after 13; or take 20 (the highest positive number) — adding other elements makes it more negative. E.g., 20, −11, 25: enter on this day and exit on that day for maximum profit; the lower negative number can be on either side, with negative numbers in between.
Q: For maximum subarray, are we using quick sort?
A: No — before maximum subarray we will do a separate algorithm; it is not a quick sort variant. On how to identify which algorithm/solution to apply: for sorting, use insertion/merge/quick depending on the number of elements and how the elements are (fairly sorted or not, size 10/20/30 → insertion; very large → quick sort and merge sort; if memory is constant → definitely quick sort).
Q: Is "balance sub array" the same kind of question as maximum subarray, with the same solution?
A: [Unclear] — probably a different concept; balance of error is not getting the maximum sum. The instructor will check and discuss separately if needed. Maximum subarray is interesting because it has three solution forms — brute force, divide and conquer, and the dynamic programming variant — with complexity varying from n² to n log n to n.
Q: Can we use dual pivot in quick sort?
A: Dual, triple, or four/five pivots are applicable in the parallel variant. There is a subject called parallel analysis of algorithms where subproblems are treated separately on different processors, requiring multiple pivots. In sequential execution, choosing multiple pivots at multiple places executes sequentially and is not much beneficial. There is a parallel variant of quick sort; multiple pivot selection is allowed there. Predefined sort methods use dual pivot in quick sort in Java and other languages — maybe they do some sort of parallelism, but we are not aware of it.
Q1. Derive the best-case and worst-case recurrences for quick sort and state the resulting complexities.
Outline: Best case $T(n) = 2T(n/2) + O(n)$ (equal split, e.g., 10 → 5 and 5) → $n \log n$. Worst case $T(n) = T(n-1) + O(n)$ (extreme pivot each time), expanded as $T(n-1)+1$, $T(n-2)+1+2$, $T(n-3)+\dots$, and structured as $T(n) = T(n-i) + i - 1 + c \cdot n$ → $n^2$. Components: pivot selection $O(1)$, partitioning $O(n)$, recursion $\log n$ in the best case.
Q2. Explain the median-of-3 pivot strategy and why it helps.
Outline: Take low (first), median index, high (last); arrange so first < median < last; replace the median with the second-last element and use it as the pivot; then run quick sort. It avoids the worst case, reducing computational time requirement by "14 plus" (as stated).
Q3. What is quick insertion and why is it faster for small arrays despite insertion sort being $n^2$?
Outline: Change the recursion base case from size ≤ 1 to size ≤ 5 (any threshold) and call insertion sort directly; e.g.,
One line per segment, [hh:mm:ss] timestamp first.
loading…