5101 - Design & Analysis of Algo
← 5101 - Design & Analysis of Algo
session: "5101-design-analysis-of-algo/2026-09-13 0000 DAA 13 Sep Lab"
title: "DAA 13 Sep Lab"
date: "2026-09-13 00:00"
duration: "1h 47m"
language: "en"
model: "deepseek-ai/DeepSeek-V4.1-Flash-1017"
parts: 2
transcript_chars: 42563
generated: "2026-11-23T23:41:55+05:30"
This lab session covered insertion sort in depth (its sorted/unsorted-part idea, the shifting mechanism under the hood, and a full code trace) and then moved to merge sort, the divide-and-conquer algorithm with its two functions (the sorting/divide function and the merge function). The recursion trace of merge sort on [4, 3, 2, 10, 12] was started and continued into the second part of the class, including the base case and the merge step. The class closed with a discussion of queues — linear vs. circular queues and priority queues — and their use in operating systems and real systems such as message brokers. It fits in the sorting-algorithms portion of the course, bridging elementary sorting (insertion sort) to divide-and-conquer sorting (merge sort) and introducing queue data structures.
key, j = i - 1, and the while j >= 0 and array[j] > key loop.i = left, j = mid + 1.Definition: The array is viewed as a sorted part and an unsorted part. The first element is taken as the sorted part; then one element at a time is taken from the unsorted part and inserted into the correct position in the sorted part by comparing with existing elements.
Why it matters: It is the baseline comparison-based sorting algorithm and the basis for the complexity discussion (O(n²) worst case, O(n) best case).
Connection: It was fully worked through (example + code trace) before merge sort was introduced as a faster-complexity alternative.
Definition: Inside the loop, elements are shifted one position right (array[j + 1] = array[j]) to make room; the key is placed only after the loop ends (array[j + 1] = key).
Why it matters: Exam-style questions ask for the array state mid-iteration; after the first shift the array contains a duplicate of the shifted element, not the key.
Connection: This is why the algorithm is called "insertion" — the key is inserted into its slot after shifting.
Definition: for i in range(1, n) with key = array[i], j = i - 1, while j >= 0 and array[j] > key, shifting inside, and array[j + 1] = key after.
Why it matters: The and condition matters: if the first condition (j >= 0) is false, the second is not checked.
Connection: The loop starts at 1 because index 0 is the initial sorted part.
Definition: Two steps: (1) divide — break the array into halves until each part has one element; (2) conquer and merge — merge the smaller sorted arrays into one sorted array (a single element is already sorted).
Why it matters: It does not sort by comparing adjacent elements; it divides, sorts smaller parts, and merges them in order.
Connection: It has two functions — the merge function and the sorting/divide function — and its recursion trace was the main second-half topic.
Definition: When left >= right, the function returns (a single element is always sorted). After the first recursive call finishes, execution jumps to the next line (the second recursive call).
Why it matters: Understanding return behaviour is essential to tracing the recursion.
Connection: The trace on [4, 3, 2, 10, 12] depends on this.
Definition: Called with left, mid, right; uses i = left, j = mid + 1, and a temporary array, comparing array[i] with array[j] and appending the smaller value.
Why it matters: This is where the actual ordering happens.
Connection: Follows the divide phase of the recursion trace.
Definition: A linear queue follows FIFO and is static (e.g., made for 10 people / 5 people); after dequeuing, freed space at the front cannot be reused, so an insert may report "full" even though memory exists. A circular queue checks whether it is full and, if not, reuses freed space for the new value.
Why it matters: Explains the motivation for the circular queue.
Connection: Introduced after merge sort as the queue data-structure topic.
Definition: Elements are inserted/processed according to priority rather than arrival order.
Why it matters: Used in operating systems, where jobs arrive with priorities and are executed according to them.
Connection: Extended into a student discussion of queues in big data, multithreading/multiprocessing, and Kafka message brokers.
for i in range(1, n) — loop over the unsorted part; starts at 1 because the first element is the sorted part.key = array[i] — the current element taken from the unsorted part to be inserted.j = i - 1 — index of the last element of the sorted part, where comparison begins.while j >= 0 and array[j] > key — the shifting condition; both conditions must be true, and if the first is false the second is not checked.array[j + 1] = array[j] — shift the larger element one position right.j = j - 1 — move the comparison pointer left.array[j + 1] = key — place the key in its correct position after the loop.mid = (left + right) / 2 — midpoint of the current subarray; used to split it into left..mid and mid+1..right.left to mid, then sort mid + 1 to right, then merge — the divide-and-conquer order.i = left, j = mid + 1 — start of the left half and start of the right half.while i < mid and j < right (as stated in the transcript) — controls the comparison loop over the two halves.array[i] > array[j]; if false, go to the else branch and append the j value, then increment j — decides which half supplies the next element.for k in range(length of temporary) … array[left + k] = temp[k] (as spoken; details [unclear]) — copies the merged temporary array back into the original array.for loop is n; a for inside a for is n(n−1), i.e. O(n²) — the student's general rule for reading complexity off loop nesting.Insertion sort narrative walkthrough on [4, 3, 2, 9, 5]:
Insertion sort code trace on [4, 3, 2, 10, 12] (n = 5, indices 0–4):
key = array[1] = 3, j = 0. Condition: j >= 0 true and array[0] = 4 > 3 true → enter loop. array[1] = array[0] → array becomes 4, 4, 2, 10, 12 under the hood (not 3, 4, …). Then j = -1; condition fails (first condition false, second not checked); loop stops. array[j+1] = array[0] = key = 3 → array becomes 3, 4, 2, 10, 12.key = array[2] = 2, j = 1. array[1] = 4 > 2 → array[2] = array[1] → 3, 4, 4, 10, 12; j = 0. array[0] = 3 > 2 → array[1] = array[0] → 3, 3, 4, 10, 12; j = -1; loop stops. array[0] = key = 2 → 2, 3, 4, 10, 12. Now the first part is sorted, the rest unsorted.Merge sort divide example on [4, 3, 2, 10, 12] (indices 0–4):
mid = 2; the left part is 4, 3, 2.Merge sort recursion trace on [4, 3, 2, 10, 12] (indices 0–4):
left = 0, right = 4: condition 0 >= 4 is false, so no return. mid = (0 + 4) / 2 = 2.0 to 2 (values 4, 3, 2); right part: 3 to 4 (values 10, 12).0, 2: 0 < 2, so no return; mid = (0 + 2) / 2 = 1. Left part 0, 1; right part 2.0, 1: mid = (0 + 1) / 2 = 0 (integer division, not 0.5). Left part 0, 0; right part 1.0, 0: condition 0 >= 0 is true → return (single element is sorted). Execution returns to the previous call (0, 1), then to the next line: call on mid + 1 to right, i.e. 1, 1.1, 1: 1 >= 1 is true → return. Both halves (4 and 3) are now individually sorted.left = 0, mid = 0, right = 1: i = 0, j = 0 + 1 = 1; temporary array taken. Check array[i] > array[j], i.e. 4 > 3 → false, so else branch: append the j value (3) and increment j.Circular queue example: queue of 5; after dequeuing 1, values 2, 3, 4, 5 remain and the front space is free; a linear queue would refuse an insert ("queue is full"), while a circular queue reuses the space (e.g., inserting 6 there).
Priority queue example: values 3, 1, 2 with arrivals — arrival 101 has priority 3, arrival 102 has priority 1. Processing order: 102 first, then 103, then 101.
Q: In-place sorting doubt — the student said insertion sort is in-place sorting, meaning "it will repeat a pattern every time."
A: The instructor explained the sorted-part/unsorted-part view and why the loop starts at 1 and j = i - 1; the student said they understood it as different from what they had in mind.
Q: Are the lab assignment links compulsory, or can they be practiced later? (One showed 6 days, and the student missed the first.)
A: All assignments are open; there are no strict timings like a week. All assignments will be closed on the 20th, so submit before that.
Q: Which sorting algorithm is most effective / most prominently used?
A: Not given in this part (the instructor moved on to the next student).
Q: Assignment 3, first question shows 0 marks.
A: There is some problem/glitch; it shows 0 even to the instructor. Keep it as submitted; marks will be allotted from their side.
Q: Comparison count vs. time complexity (merge vs. insertion) — the student was confused because merge sort seems to do more comparisons while its complexity is better.
A: The instructor initially thought the question was about merge sort, then clarified insertion sort had just been completed; the student's question was not resolved in this part.
Q: Can two for loops replace the while?
A: Yes — you can use for i in range(1, length) and an inner for j in range(i, 0, -1) (step −1); it's easy, you don't have to use while.
Q: How to calculate time complexity when there is a while inside a for?
A: For insertion sort we calculate three types; worst case is O(n²), best case is O(n); the student was referred to a video (Abdul Bari) to see the calculation.
Q: Test coverage failing on submission — the insertion sort runs and gives the sorted array but all test cases fail on submit.
A: The instructor asked which test cases fail and said they will check it.
Q: At the midpoint where you have 4 and 3, can we instead keep a single element 4 on the left and 3 and 2 on the right — can we divide that way at this division itself?
A: The instructor acknowledged the question; noted that the overall algorithm is understandable but one gets lost in the recursive function; said no worries, he will take another (bigger) example in the next class.
Q: In big data / multithreading / multiprocessing, when we process tasks one by one using a queue, is it really scalable for real-time/business use cases?
A: Inside the job, libraries (threading, async functions) use these structures — e.g., priority queues — abstracting those layers and exposing only part of it.
Q: Kafka publishes messages to a topic, stored in a message broker which internally uses a queue — is it the same queue we are discussing?
A: The instructor could not confirm ("I'm not able to tell you, sorry"); thinks they use the same fundamentals to scale, and that it is all wrappers that parallelize — "possible." The student noted he knew the async/synchronization/threading parts from OS but not the scalable-system side.
Student remark: Understood merge sort overall and the algorithm; implementation via the Python code is fine, but the recursive function is where one gets lost.
i = 1 (not 0) because index 0 is the initial sorted part; j = i - 1.j >= 0 and array[j] > key; if the first condition is false, the second is not checked.array[j + 1] = array[j]), it does not swap; the key is placed only after the loop.left >= right → return (a single element is sorted); after the first recursive call returns, execution jumps to the second recursive call.i = left, j = mid + 1, and a temporary array, appending the smaller of array[i] and array[j].Q1. Trace insertion sort on [4, 3, 2, 10, 12] for the first two iterations, showing the array state after each shift.
Outline:
One line per segment, [hh:mm:ss] timestamp first.
loading…