classnotes RTX 4070 Ti SUPER · faster-whisper · DeepSeek

5101 - Design & Analysis of Algo

← 5101 - Design & Analysis of Algo

DAA_13 Sep_Lab

Date 2026-09-13 Length 1h 47m Words 8,893 Segments 730
⤓ Download transcript (.txt with timestamps) ⤓ Plain transcript (.txt) ⤓ Transcript (.md)

Summary


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"


DAA 13 Sep Lab

Overview

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.

Learning outcomes

Core concepts

Insertion sort

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.

Under-the-hood behaviour: shifting, not swapping

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.

Insertion sort implementation

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.

Merge sort (divide and conquer)

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.

Merge sort base case and recursion return

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.

The merge step

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.

Linear queue vs. circular queue

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.

Priority queue

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.

Formulas and maths

Worked examples

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):

Merge sort divide example on [4, 3, 2, 10, 12] (indices 0–4):

Merge sort recursion trace on [4, 3, 2, 10, 12] (indices 0–4):

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.

Questions asked in class

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.

Key takeaways

Exam-style questions

Q1. Trace insertion sort on [4, 3, 2, 10, 12] for the first two iterations, showing the array state after each shift.

Outline:

Transcript

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

loading…