classnotes RTX 4070 Ti SUPER · faster-whisper · DeepSeek

5101 - Design & Analysis of Algo

← 5101 - Design & Analysis of Algo

DAA_26 Sep

Date 2026-09-26 Length 1h 21m Words 10,610 Segments 752
⤓ Download transcript (.txt with timestamps) ⤓ Plain transcript (.txt) ⤓ Transcript (.md)

Summary


session: "5101-design-analysis-of-algo/2026-09-26 0000 DAA 26 Sep"

title: "DAA 26 Sep"

date: "2026-09-26 00:00"

duration: "1h 21m"

language: "en"

model: "deepseek-ai/DeepSeek-V4.1-Flash-1017"

parts: 2

transcript_chars: 51454

generated: "2026-11-23T23:46:09+05:30"


DAA 26 Sep

Overview

This class completed the treatment of the maximum subarray problem: after a brief revision of quicksort (median-of-3 pivot selection and its 14 percent reduction in computational requirement) and the key differences between quicksort and merge sort, the session motivated the maximum subarray problem from a stock-market scenario, derived the brute-force solution (order of n square, order of n cube if ordered subsets are considered), and developed the divide-and-conquer solution via left sum, right sum and cross sum. A full divide-and-conquer trace was worked on a 16-element array, then re-examined to clarify how the cross sum is bounded by the extreme indices of the left and right sums, and finally walked through on a smaller 8-element array. The class sits in the divide-and-conquer portion of the course, with Kadane's algorithm and the dynamic-programming treatment explicitly deferred.

Learning outcomes

Core concepts

Median of 3 (quicksort pivot selection)

The three elements considered are the first, middle and last element; they are arranged so that the required order (first should be less than ...) is followed. It matters because it prevents worst-case behaviour in a nearly sorted array and helps maintain nearly equal partitions, reducing the computational requirement by 14 percent. It connects to the recurrence t(n) = 2t(n/2) + order of n, which gives n log n only for balanced partitions.

In-place operation and cache friendliness (quicksort vs merge sort)

In quicksort the same memory location is used; merge sort requires extra memory and stores the result at a different place. Quicksort is cache friendly; merge sort has more memory operations. Cache friendly means no extra space is primarily required. This connects to the practical comparison of the two sorting algorithms revised at the start of class.

Maximum subarray problem

Given an array containing both positive and negative numbers, identify a subarray whose sum is highest. It is inspired by a real-world stock-market scenario: buy a share on a certain day and sell it after some day (the share remains blocked for some days); if you enter on a day when the price is very low and sell on a day when the price is very high, the profit margin becomes maximum. It is called a "problem" because it is a task posed in practical scenarios (e.g., share trading, maximum quarterly earnings), like sorting or searching problems. It is the running example for the divide-and-conquer technique.

All-positive / all-negative cases

If all elements are positive, the sum of all elements is the maximum sum. If all are negative, pick the least negative single number. The problem is really interesting when the array contains both positive and negative numbers.

Brute force (maximum subarray)

Check all possibilities and identify whichever is highest. When taking values a and b, the pair b, a is not taken as a subset — order is not bothered about because the sum remains the same. This wisely removes duplicate entries/subsets and reduces complexity from order of n cube to order of n square; if order were considered (a, b and also b, a), complexity reaches order of n cube. The laptop analogy: brute force searches every space in the house, garden, washroom, etc.; applying a little heuristic/intelligence restricts the search to the living area, study and bedroom, raising the chance of finding it and removing irrelevant work.

Divide and conquer

Divide, conquer, combine. Partition the array, solve on the left and right, and stop the recursive division when a single-element level is reached. The formal algorithm will be seen at the later stage.

Left sum, right sum, cross sum

Left sum: adding the entries of the left side and picking out the maximum. Right sum: similarly, pick the max on the right side. Cross sum: identify the middle (median) part; from the median go toward the left and identify the maximum possible sum, from the median go toward the right and identify the maximum sum, then add them. Cross sum does not mean taking all entries of the left side and all entries of the right side and adding them. The maximum subarray may reside on the left part, the right part, or the cross section.

Bounding the cross sum

The cross sum is computed only between the index where the left-side maximum was found and the index where the right-side maximum was found; going beyond that gives no significant difference and defeats the purpose of the algorithm. This is "not an assumption, this is the algorithm part." Indices are tracked so that at the top level the algorithm knows to what extent it must take values and find the sum.

Propagation across recursion levels

The cross sum computed for a subproblem becomes the left sum (or right sum) for the larger array at the level above: "a cross sum becomes a left sum when we proceed upward." The highest cross sum at a level becomes the left sum for the level above.

Median consistency

Whichever median convention is used (n/2 or n/2+1) must be used consistently at all levels; changing it midway gives a different intermediate result. The claim made is that the final answer (e.g., 25) is the same either way, but this was to be verified by solving a problem with n/2+1 as median.

Dynamic programming (mentioned)

All dynamic programming algorithms fasten processing by taking extra space; previous results are stored and used to get the next step result. In divide and conquer the stored values are only temporary, not fixed/preserved for the entire process.

Formulas and maths

Symbols: t(n) is the running time on n elements; the two recursive calls are on halves; O(n) is the partitioning cost.

Used for balanced partitioning in quicksort — only in this case is the complexity n log n; otherwise it may reach n square when the array is already sorted.

Symbols: 14 percent is the reduction in computational requirement from median-of-3 pivot selection.

Used when arguing the practical benefit of median-of-3 over a naive pivot.

Symbols: i and j are loop indices over the array a; sum accumulates the running subarray sum; max holds the best sum found.

Used for the brute-force maximum subarray solution; loop inside loop gives complexity order of n square.

Symbols: low and high are the current subarray bounds; mid is the split point.

Used in the divide-and-conquer split; the same is applicable for odd-length arrays.

Symbols: the two terms are the best sums extending from the midpoint outward on each side.

Used to handle the case where the maximum subarray crosses the midpoint.

Symbols: the extreme indices mark where the best left and right sums were found.

Used to bound the cross-sum computation.

Symbols: mid, low, high as above.

Used when scanning outward from the midpoint to compute the cross sum.

Worked examples

Quicksort on sorted input: standard example 10, 20, 30, 40, 50 — already sorted; number of elements counted as 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. Total comparisons required: 45; worst-case behaviour is order of n. With the other strategy the total comparisons required is 39 — 6 less when the number is only 10; when the number is 100 it is 60 less. This shows median of 3 reduces complexity from order of n square toward n log n.

Brute force on 2, −6, −1, 3, −1, 2, 3:

Brute force loop trace on 2, −6, −1 (six numbers, i = 1): sum = 2; max was 0, so max = 2. j = 2: sum = −4, max stays 2. j = 3: sum = −5, max stays 2. j = 4: sum = −2, max stays 2. j = 5: sum = −4, max stays 2. j = 6: sum = −9, max stays 2. Then i = 2, j runs 2 to 6, and so on.

Divide and conquer trace on 16 elements: 13, −3, −25, 20, −3, −16, −23, 18, 20, −7, 12, −5, −22, 15, −4, 7 (low = index 1, high = index 16).

Large example (revisited):

Small 8-element example:

Student's alternative attempt (median at 18):

Questions asked in class

Punit: why not use Kadane's algorithm instead?

Maximum sum can be solved via brute force, divide and conquer, and dynamic programming; Kadane's algorithm will be discussed when dynamic programming is covered. Dynamic programming's space complexity is a little higher than divide and conquer, but it is famous for maximum subarray; all dynamic programming algorithms fasten processing by taking extra space — storing previous results in memory and using them to get the next step result. Here also previous results are stored after getting the median, but these are just temporary, not fixed; in dynamic programming

Transcript

One line per segment, [hh:mm:ss] timestamp first.

loading…