classnotes RTX 4070 Ti SUPER · faster-whisper · DeepSeek

5101 - Design & Analysis of Algo

← 5101 - Design & Analysis of Algo

DAA_19 Sep

Date 2026-09-19 Length 1h 34m Words 10,727 Segments 713
⤓ Download transcript (.txt with timestamps) ⤓ Plain transcript (.txt) ⤓ Transcript (.md)

Summary


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

title: "DAA 19 Sep"

date: "2026-09-19 00:00"

duration: "1h 33m"

language: "en"

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

parts: 2

transcript_chars: 54022

generated: "2026-11-23T23:43:30+05:30"


DAA 19 Sep

Overview

This class recaps divide-and-conquer sorting and searching — merge sort (recursive division, the two-pointer merge, and its recurrence) and binary search (sorted-array assumption, halving, worked searches) — before introducing quick sort as a partition-exchange algorithm. The quick sort partition procedure is dry-run in detail on a ten-element array, followed by analysis of its worst, best and average cases, pivot-selection strategies (including median-of-three), handling of duplicates, and a comparison with merge sort. The class closes with student questions on library sort implementations, real-world use (B-trees/B+ trees, databases), and recursion versus iteration, and previews the maximum subarray problem.

Learning outcomes

Core concepts

Divide and conquer

A recursive division approach where a large problem is divided into multiple sub-problems; division continues until the array has one element; then the conquer step solves the sub-problems and combines their solutions to form the final solution. It is the shared paradigm behind merge sort, binary search and quick sort, and frames the course's progression from brute force to divide and conquer (and later dynamic programming).

Base condition (base case)

If the number of elements in an array is less than or equal to 1, that is the base condition of the recurrence relation and where we must stop. The recursion keeps recurring to reach the base condition. Without it, the recursion would never terminate.

Merge sort structure and partitioning

Merge sort has two components — the recursive merge call and the merge operation. In the recursive call, if length ≤ 1, return the array (size becomes 1); otherwise middle = length of array by 2, split into left half and right half; when both halves have one entry each, the algorithm stops and calls the merge operation. Partitioning criteria can use n by 2 int, n by 2 float, or n by 2 ceiling values. Division itself does nothing (just n by 2, n by 4, n by 8, … until single elements); the merge operation is what imposes computational complexity of order of n.

Merge procedure (two-pointer merge)

Declare an empty list (result); initialize left index and right index to zero; while left index < length of left and right index < length of right, compare first element of left with first element of right; append the smaller to result and increment the corresponding index. The last two steps extend the remaining part of whichever sub-array still has elements — this works because the sub-arrays being merged are already sorted.

Binary search

All elements must already be sorted — an important consideration and a drawback. Go directly to the middle (n by 2), check whether to go left or right depending on the value of the key; every time the search space is reduced by half (1 by 2, 1 by 2, …), reaching the solution in log of n comparisons or declaring the element is not present. Decision rule: if k is equivalent to a[n by 2], we are done; if k is less than middle, search on the left; otherwise search right. Sorting is a prerequisite for binary search; the two are correlated because both follow divide and conquer.

Quick sort: partition exchange algorithm

Also a divide and conquer strategy based approach; known as the quickest/fastest sorting algorithm; also known as a partition exchange algorithm — it does not directly do n by 2, floor n by 2, or ceiling n by 2. The target is to freeze/fix the position of the selected pivot in each iteration. The pivot could be a random element; generally the 0th index element (simplest case: first element as pivot). Choosing the right pivot is very important and affects the complexity; a popular mechanism is median of three. Structure: if low < high, do partitioning of a into low and high, then recursively call quick sort on the two sub-arrays; pivot position is fixed after one iteration.

Partition procedure and invariant

The pivot is the first element (A of low); the immediate right index is treated as left, and the last index is treated as right. Invariant: all elements to the left of the pivot should be less than the pivot, and all elements greater than the pivot should be to the right of the pivot; if at any point these conditions are violated, the direction of scanning is changed.

Pivot selection and cases

Choosing an extreme element (smallest or largest) gives one sub-array of more elements and the other of zero elements, leading to the worst case. Worst case: every time the extreme element is chosen as pivot; sub-array sizes go n−1, n−2, n−3, … (each time one element is "curtailing down"). Best case: pivot is the middle element (in most cases); average case: pivot is near the middle. Methods mentioned: first element; random selection (randomized version of quicksort); median (n by 2, ceiling or floor — as done in binary search); median-of-three partition.

Median-of-three

Choose first, middle, and last elements; ensure the order first < middle < last by performing swaps if the order is violated; then use the median as pivot. It eliminates the bad cases of sorted input and reduces the number of comparisons by 14 percent. Steps as stated: center is left by 2 (center = left + 2 per transcript wording "center is left by 2"); if A of center is less than A of left, swap; if A of right is less than left, swap; if A of right is less than center, swap. Then place the pivot at position right minus 1 (swap with the second-last element) and start quicksort.

Duplicates in quick sort

The inner while loops as stated have no equality sign; to handle duplicate elements, use a minor modification — use the equality sign on either the left sub-half or the right sub-half: A of left is less than or equal to pivot, or A of right is greater than or equal to pivot.

Quick sort vs merge sort

Comparison count is of the same order (order of n) in both; merge sort divides down to the smallest elements, quicksort picks a pivot. Quicksort is in-place (result stored in the same array; merge sort uses another array), does not require swap-in/swap-out with external memory, and is the fastest known algorithm in practice for average/best cases. Swaps incur cost, but it is much lower than the dominating cost of the algorithm; the dominating operation must be identified. Quicksort is used primarily for main-memory operations, not databases (databases use designated trees).

Recursion vs iteration

Each recursive function call is stored in the call stack (internal memory) which vanishes after execution; removing recursion (using loops/iteration) removes that memory requirement. In quicksort the array and result are stored in the same array, whereas in merge sort the result is stored in another array.

Brute force vs paradigms

Insertion sort comes under brute force; divide and conquer, dynamic, and greedy are the main paradigms; brute force is also a paradigm, though in some literature it is not assumed to be an algorithmic paradigm.

Formulas and maths

Worked examples

Merge example

Left and right sub-arrays with elements 1, 3, 4, 5, 6 (length 3 each); result declared as empty list, left index = right index = 0.

Another merge example mentioned: 1 2 7 3 4 5 6 — if no element remains in the left sub-index and some remain in the right, the while loop fails and the right index portion is appended.

Binary search example 1 (search for 33)

Array 6, 6, 13, 14, 25, 33, 43 (sorted ascending), search for 33. Middle: 14 by 2 = 7, so 0 to 7; compare 53 with 33 → 53 is greater, so the element lies toward the left. Now search 33 in six elements (0 1 2 3 4 5 6); middle gives 25; 33 must be on the right of 25. Problem size reduced from 14 to 6, then to 3, then to 2. Middle element 43 compared with 33 → 33 lies in its left; one element remains (low, high, and middle all the same) — the result. Only three comparisons (with 53, 25, 43) plus the last comparison with 33, i.e., four comparisons, identify the key.

Binary search example 2 (search for 20)

Indexes 0 to 6, n by 2 = 3, compare 20 with 100 → 20 lies in its left. Remaining 10, 20, 50 with indexes 0, 1, 2; 2 by 2 = 1; compare 20 with 20 → found. Result obtained in only 2 comparisons.

Quick sort dry run (pivot 8)

Array indices 0–9; pivot = 8 (first element, A of low); left = index 1 (value 12), right = index 9 (value 6).

1. Left check: 12 is less than pivot 8 — condition fails. Right check: A of 9 = 6 is greater than 8 — condition fails. left < right, so swap 12 and 6: 6 comes to the left position, 12 goes to the right position.

2. Main while loop again (left = 1 < right = 9): A of 1 = 6 < 8 → left incremented to 2. A of 2 = 14 < 8 fails. Right side: A of 9 = 12 > 8 → right decremented to 8. Left encounters 4 (not greater than 8 — condition fails). left = 2 < right = 8, so swap 14 and 4: 4 comes in place of 14, 14 goes to the other position.

3. Next: 15 and 9 are swapped (A of left = 15 is not less than 8, so direction changes; on the right, 9, 11, 10, 13, 15 are all greater than 8 so right keeps moving, and stops when it encounters 4, which is not greater than 8).

4. Now left is greater than right, so the main while loop terminates; A of right is swapped with the pivot.

5. Result: 4, 6, 8 (pivot), 15, 13, 10, 11, 9, 14, 12 — the position of 8 is now fixed.

Quick sort second iteration (pivot 15)

Take the array with indices 0–9; choose 15 as pivot; 13 becomes left and 12 becomes right. 13 < 15 → left moves ahead; 10 < 15 → moves; 11 < 15 → moves; 9 < 15 → moves; 14 < 15 → moves; 12 < 15 → moves and stops at the final state. No while loop execution because conditions fail; left is not less than or equal to right, so the main while loop terminates. Result: 13, 10, 12 are there (swap between pivot and the first element), 11, 9, 14, and 15 — now the fixed location of 15. Remaining: 12, 13, 10, 11, 9, 14; apply quicksort again choosing 12 as pivot, which goes to its fixed position.

Quick sort worst case

Array 1, 2, 3, 4, 5, 6, 7, 8 (already sorted). Choosing the index-0 element as pivot: its position remains fixed, then n−1 elements remain, then n−2, then n−3 — every time the extreme part (index 0 element) is cut down; the inner while loop runs n−1 times → order of n square instead of order of n log n.

Median-of-three example

Array 2, 13, 6 — after applying the algorithm steps, 6 comes to the middle position and 13 goes to the other position; 6 is selected as pivot and swapped with the second-last element, then quicksort starts.

Questions asked in class

Q: Merge sort and binary search have separate purposes; is merge sort a prerequisite before binary search can be done?

A: Yes, any sorting is a prerequisite for binary search. They were being correlated because both follow divide and conquer — both divide the problem/search space into smaller parts.

Q: In real applications we just call a sort method (Python, Java); how does the language decide which sorting algorithm to use, given different complexities?

A: By default there is a specific fixed sort function installed; one can search online or with an AI tool to find which sorting it actually uses. If writing your own function with insertion/merge/quick sort, you would write conditions: e.g., if n is 20 call insertion sort, if 100 call merge sort; if space is available use merge sort, if not use quick sort; if elements are fairly sorted, do not prefer quick sort. In the instructor's knowledge, a specific algorithm runs on behalf of every sub-problem supplied.

Q: Does insertion sort (and the iterative search done before) come under brute force?

A: Yes, insertion sort comes under brute force; now the course has moved to divide and conquer; later dynamic programming will be covered. In some literature brute force is not assumed to be an algorithmic paradigm, but the main paradigms are divide and conquer, dynamic, greedy, and others; brute force is also there.

Q: How are these concepts used in real time?

A: Example given: B-trees / B+ trees used in databases with multi-level hashing, where at each step a search must be performed; searching is used when retrieving a document from secondary storage to primary storage, and sorting is needed whenever we must arrange/place elements so we can reach the solution or label as fast as possible. Searching and sorting are not the primary operation in any memory system — the primary operation is reaching the records as soon as possible.

Q: For binary search, the initial criterion is that the array should always be sorted — is that a drawback?

A: Yes, always sorted is a drawback of binary search; time complexity is always considered only for the searching part, not for sorting.

Q: The assignment is not showing in the assignment part.

A: The instructor has uploaded it but the team may be delaying at their end; assignment number two will be uploaded in a couple of days.

Q (Bikesh): We fix the pivot's position and partition the array into two halves, and

Transcript

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

loading…