5101 - Design & Analysis of Algo
← 5101 - Design & Analysis of Algo
session: "5101-design-analysis-of-algo/2026-09-27 0000 DAA 27 Sep Lab"
title: "DAA 27 Sep Lab"
date: "2026-09-27 00:00"
duration: "1h 50m"
language: "en"
model: "deepseek-ai/DeepSeek-V4.1-Flash-1017"
parts: 2
transcript_chars: 49803
generated: "2026-11-23T23:48:48+05:30"
This class completed the divide-and-conquer sorting unit: it walked through merge sort's divide and merge steps with a full dry run on an 8-element array, then developed quick sort from the pivot/partition idea through the recursive calls and a code dry run. The session then moved to two new algorithms — maximum subarray sum (Kadane's algorithm, brute force vs. single pass) and binary search — and closed with course logistics (hidden test cases, a lab extension, feedback link). It sits after the introduction of recursion and divide-and-conquer and before the upcoming "cadence" algorithm and further binary search work.
mid, split into left/right, apply the base condition, and merge two sorted halves step by step.length <= 1 while quick sort's is low > high.i, swap smaller elements forward, and place the pivot in its final position.start:end, step) and tuple swapping without a third variable.Divide the array into two halves, recursively sort both halves (left side first, then right side), then merge using a merge function that takes only the left and right parts; leftover values are appended to the result. The base condition is: if the length of the array is <= 1, return the array — it exists because the function recursively calls itself and needs a stopping point. This is the reference divide-and-conquer sort against which quick sort is contrasted.
Starts with i = 0, j = 0 and a result array; compares left[i] with right[j]; if left[i] < right[j] it appends the left value and increments i, otherwise it appends the right value and increments j. After the main loop, remaining (already sorted) elements are appended without further comparison. append adds an element to the last position of the array. This is the "combine" half of merge sort.
array[start:end] goes from start up to end; with no start/end given it goes from the first element to the last. A step value skips elements — e.g. on 0 1 1 2 3 with step 1 starting from 0, only 0 and 2 are picked (it skips one value each time). Slicing is how the merge sort code produces the left and right halves.
Also divide and conquer, but the major difference is that merge sort divides first and sorts while merging, whereas quick sort places a pivot in its correct position first, then recursively sorts the elements on its left and right. The pivot is "like a center position" — any element chosen as the pivot; it is placed into its correct (final sorted) position. Philosophy: choose the pivot, put everything smaller on the left side and everything bigger on the right side, then quick sort the left side and then the right side.
After the first partition, the pivot is in its final sorted position; everything to its left is smaller and everything to its right is larger, but the two sides are not necessarily sorted yet. Boundary i represents the boundary of elements that are smaller than or equal to the pivot; initially there are no such elements, so i = -1. The partition explanation is marked as "very important."
Quick sort checks if low > high (unlike merge sort, which checks length ≤ 1), because it checks from both sides; if the indices have crossed/swapped, it returns. low and high are indices only (not arrays) — the call is made with 0 and length of array - 1. After partitioning returns the pivot index, recursively sort the left side with (array, low, pivot index - 1) and the right side with (array, pivot index + 1, high). There are two separate functions: one is the partition, the other is quicksort. Pivot choice is flexible: it can be the first element or any element, not necessarily first or last.
Done directly as array[i], array[j] = array[j], array[i] — no third variable needed (unlike C/C++). Used throughout the partition step.
Find the contiguous subarray with the maximum sum. Brute force maintains a result and uses two loops (one over the whole array, one over the internal array), comparing results; time complexity around 9 × 9 = 81 for 9 elements, which is "not cool." The single pass maintains a current sum (sum up to now) and a global/maximum sum, updating both in one pass to keep time and space complexity low.
Searching by looking at the middle and eliminating half of the array each time, instead of checking every element (linear search). It only works for a sorted array. Uses three pointers: low, mid, high. Described as "the easier one."
mid = length of array // 2 — mid is the split index, // is Python integer division (not plain /); used to divide the array in merge sort.array[0:mid], right part = array[mid:] — start to mid, and mid to last; used to form the two halves in merge sort.while i < len(left) and j < len(right) — i, j index the left/right halves; with and, if one condition fails the second is not checked and the loop exits; this is the merge loop condition.while i < len(left): result.append(left[i]) (and similarly for the right side) — appends leftover already-sorted elements after the merge loop.quicksort(array, 0, len(array) - 1) — initial call; low = 0, high = last index; used to start quick sort.pivot = array[high] (last element); i = -1; for j in range(low, high) — j runs up to high - 1, excluding the pivot; if array[j] <= pivot: i = i + 1, swap array[i] with array[j]; this is the partition loop.array[i+1] with array[high]; return i + 1 as the pivot index — places the pivot in its final position and reports it.quicksort(array, low, pivot_index - 1) and quicksort(array, pivot_index + 1, high) — recursive calls on the left and right of the pivot.i = low - 1 — partition initialization when the pivot is the high element and no smaller element has been found yet.current sum = max(array[i], current sum + array[i]) — array[i] is the current element; decides whether to extend the running subarray or restart at the current element; used in Kadane's single pass.max sum = max(max sum, current sum) — keeps the best sum seen so far; used in Kadane's single pass.for i in range(1, length of array) — starts at 1 because the 0th element is taken as the initial value; range goes till length − 1; the Kadane loop.low = 0, high = length of array - 1 — initial binary search bounds.while low < high (as spoken; note the transcript says "while low is less than high") — binary search loop condition.mid = (low + high) / 2 — middle index in binary search.array[mid] == target → return mid (the index); array[mid] < target → low = mid + 1; else → high = mid - 1; if not found → return -1 — the binary search decision branches.Merge sort dry run on [10, 7, 8, 9, 1, 5, 3, 2] (8 elements; the transcript also refers to the array as 1, 2, 3, 6, 7, 8-style values — the concrete values used in the run are the ones below):
mid = 8 // 2 = 4.[10, 7, 8, 9]; right = mid to last → [1, 5, 3, 2].mid = 4 // 2 = 2 → left [10, 7], right [8, 9].[10, 7]: length 2, mid = 2 // 2 = 1 → left [10], right [7]. Each has length 1, so each returns (base condition).[10] and [7]: i = 0, j = 0; is 10 < 7? False → append right value 7, j = 1. Right side exhausted; leftover loop appends 10. Result [7, 10].[8, 9] is divided and merged similarly (transcript: "it will compare and then it will merge", returning 8, 9).[7, 10] with [8, 9]:7 < 8 → append 7, i = 1.10 < 8? False → append 8, j = 1.10 < 9? False → append 9, j = 2. Right side finished (length 2), loop exits.10. Result [7, 8, 9, 10].Quick sort example on [8, 3, 5, 4, 7, 6, 1, 2] with the last element 2 as pivot:
[1, 3, 5, 4, 7, 6, 8, 2].array[i+1] (3) with array[high] (2): array becomes [1, 2, 5, 4, 7, 6, 8, 3].Quick sort code dry run on the same array (call quicksort(array, 0, 7)):
low = 0, high = 7, not low > high → call partition with (array, 0, 7).array[7] = 2; i = -1; j runs over range(0, 7) i.e. 0 to 6 (pivot excluded).8 < 2? No. … at j = 6, 1 < 2? Yes → i = -1 + 1 = 0; swap array[0] (8) with array[6] (1) → [1, 3, 5, 4, 7, 6, 8, 2].array[i+1] = array[1] (3) with array[high] = array[7] (2) → [1, 2, 5, 4, 7, 6, 8, 3].i + 1 = 1 → pivot index = 1; recursion proceeds on (low, pivot_index - 1) and (pivot_index + 1, high).Quicksort — right-side recursion and partition dry run:
(array, 0, 1 - 1 = 0) → low = 0, high = 0; 0 is not greater than 0, so it returns (base condition).(array, pivot index + 1, high) = (array, 2, 7).(2, 7): low = 2, so i = low - 1 = 1; pivot = array[high] = array[7] = 3.j from 2 to 6 (pivot at index 7 is not checked):array[2] = 5, 5 < 3 → falsearray[3] = 4, 4 < 3 → falsearray[4] = 7 → falsearray[5] = 6 → falsearray[6] = 8 → falsearray[i + 1] = array[2] with array[high] = array[7], i.e., 5 swaps with 3 → array becomes 1, 2, 3, 4, 7, 6, 8; return i + 1 = 2.(array, 2, 1) → 2 > 1, so it finishes; then the right side (3, 2, 7) is processed, again finding the pivot (last element), comparing with all elements, swapping smaller elements with the greater one and then into its correct position, and returning its position.Kadane's dry run (array begins with −2, 1, −3, 4, ...):
current sum = -2, max sum = -2.i = 1: array[1] = 1; current sum = max(1, -2 + 1) = max(1, -1) = 1; max sum = max(-2, 1) = 1.i = 2: array[2] = -3; current sum = max(-3, 1 + (-3)) = max(-3, -2) = -2; max sum = max(1, -2) = 1.i = 3: array[3] = 4; current sum = max(4, -2 + 4) = max(4, 2) = 4; max sum = max(1, 4) = 4.i = 4: array[4] = -1; current sum = max(-1, 4 + (-1)) = max(-1, 3) = 3; max sum = max(4, 3) = 4.current sum = max(2, 3 + 2) = max(2, 5) = 5; max sum = max(4, 5) = 5.i = 6: array[6] = 1; current sum = max(1, 5 + 1) = 6; max sum = max(5, 6) = 6.array[0] — any element (e.g., array[7], or −1) can be used.Binary search example:
2, 5, 8, 16, 93, 58, 58 (as spoken); target = 23.23 > 16, and since the array is sorted, the left half (2, 5, 8, 12, 16) cannot contain 23 → discard it.23 < 38 → discard that half; element 23 is found.i + 1), which is assigned to the pivot index variable because the left and right sides must be sorted according to it — that is why the pivot index is needed. The student had thought there were two different variables; clarified that there are two different functions (partition and quicksort).
One line per segment, [hh:mm:ss] timestamp first.
loading…