IITP · Semester ISummaries · Quizzes · Assignments

Lecture summary

DAA 04 Oct lab

04 Oct 20261h 20m2 parts

Source: IITP-big-files\Sem-1\ECS5101-DAA\Lectures\2026-10-04 0000 DAA 04 Oct lab\summary\notes.md

Overview

This class covered bubble sort in detail — its definition, why it is called "bubble" sort, passes and comparisons, counting swaps, writing the code, and tracing it on small examples — and then moved to the closest pair of points problem using a divide-and-conquer approach (left half, right half, cross pairs). A platform issue with sample test cases not visible while running code was raised and fixed. The later part of the session surveyed AI/DS learning resources: vision-language models and vision transformers, interactive neural network and CNN visualization demos, image generation origins (diffusion, Brownian motion, energy-based models), and recommended books/sites. The class ended with an unanswered student question about the instructor's approach to the algorithm problem, deferred to later.

Learning outcomes

  • Explain what bubble sort is, why it is called "bubble" sort, and what a pass is.
  • Write bubble sort code with the inner loop bound n - i - 1 and a swap counter num_swaps.
  • Trace bubble sort pass by pass on a small array and state which element is fixed after each pass.
  • Define the closest pair of points problem and compute Euclidean distance between two points.
  • Outline the divide-and-conquer approach for closest pair: left minimum, right minimum, cross-pair minimum.
  • Describe what the neural network, CNN, and convolution visualization demos show (learning rate, activation, filters, max pooling, padding).

Core concepts

Bubble sort

A simple sorting algorithm that repeatedly compares adjacent elements and swaps them if they are in the wrong order (if the left element is greater than the right element, swap them). It matters as the baseline sorting algorithm for the course and the basis of the swap-counting problem. It connects to the lab challenge where students count the number of swaps needed to sort an array.

Why "bubble"

During every pass, the largest unsorted element gradually "bubbles up" to the end of the array. Analogy given: in water, the larger bubble goes up first; also compared to the film Secret Superstar, where bubbles go up. This explains the name and why the last position is fixed after pass 1.

Pass

One full sweep of adjacent comparisons. After pass 1, the largest element is guaranteed to be in its correct (last) position. There is no guarantee the array is sorted after pass 1, so passes continue until the array is sorted.

Not re-comparing sorted elements (better version)

After each pass, one more element is already in its correct position, so it does not need to be compared again. This is why the inner loop uses n - i - 1 — the "better version" of bubble sort. The n - 1 bound exists to avoid comparing out of index (array out of index error), since each element is compared with its adjacent (next) element.

Swap counter

A variable num_swaps initialized to 0, incremented by 1 after each swap, to count how many swaps happen during the sort. This is the quantity the lab problem asks for.

Closest pair of points problem

Given n points, each represented by coordinates x and y, find the minimum Euclidean distance between any two distinct points (not the same point). It motivates the divide-and-conquer technique covered next.

Euclidean distance

The distance between (x1, y1) and (x2, y2) is the square root of (x2 − x1)² + (y2 − y1)². It is the distance measure used throughout the closest-pair algorithm.

Divide-and-conquer for closest pair

Divide the points into left and right halves; compute the minimum distance within the left half and within the right half; compare the two to get the minimum. This is not sufficient by itself — pairs that cross the dividing line must also be checked, and if a cross-pair distance is smaller, it becomes the new minimum. The points are sorted by coordinates (the reason is stated as something to be explained: "I'll tell you why I'm sorting").

VLM and vision transformers

The instructor notes the earlier material was likely for the LLM, and this part is for the VLM, where the vision transformer starts — you are given an image size of a section/cross-section and pass that image into other transformations.

Neural network basics and demos

Basics to know: learning rate, activation function, regularization, and problem type (classification or regression). In the classification demo (classifying between two points), you can watch the error/training loss going down and see how many epochs it takes to classify; you can see which neuron/what part is classified and play with activation functions (e.g., sigmoid) and depth.

CNN demo and filters

A multi-class digit classification demo (digits 0, 1, 2, 3, 4, 7, 8, 9): drawing an 8 is properly classified as an 8. The visualization shows how each layer detects edges — the first layer calculates certain edges, the second layer calculates other edges and the below edges, the third layer calculates upper edges, while also reducing the inner size, and finally it is classified as an 8. A filter is used to calculate edges: a top-edge filter produces the red part (top edge); a right-edge filter produces the red side on the very right. You do not need to memorize values like minus one, zero, minus one, zero — you can look them up. The image is converted into zeros and ones — only two channels, not RGB; zero means black, one means white.

Max pooling and padding

Max pooling is "nothing but reducing the image size" — making it smaller. The instructor asks you to question whether reducing the image matters, since a high-resolution image would clarify better for the machine than a lower-resolution one, and to ask GPT what the reason behind reducing is. Padding is a method to avoid cutting down the image — "padding add, reduce ho ke bhi" (even after reducing, add padding), so the image is not cut down.

Image generation origins

Image generation came from physics — diffusion, and before diffusion, Brownian motion — introduced into "image energy" things, which are also called energy-based models. Knowledge distillation and dropout are attributed to the "godfather of AI"; the instructor calls this a very, very important website.

Formulas and maths

  • Bubble sort comparison/swap condition: array[j] > array[j+1] — array is the list being sorted, j the inner-loop index; used to decide whether adjacent elements must be swapped.
  • Swap code: array[j], array[j+1] = array[j+1], array[j] — exchanges the two adjacent elements; used whenever the condition above is true.
  • Inner loop bound (better version): for j in range(n - i - 1) — n is the array length, i the pass index; used so already-sorted tail elements are not re-compared and no out-of-index access occurs.
  • Euclidean distance: sqrt((x2 - x1)^2 + (y2 - y1)^2) — (x1, y1) and (x2, y2) are the two points; used to measure the distance between any pair of points in the closest-pair problem.
  • Euclidean distance in code: dx = p1[0] - p2[0], dy = p1[1] - p2[1], return math.sqrt(dx*dx + dy*dy) — p1, p2 are point coordinates; used as the distance function in the code sketch.
  • Cross-pair check condition (as spoken): if the vertical difference between points is greater than d, break; otherwise compute the distance and take the minimum — d is the current minimum distance; used to prune cross-pair checks.

Worked examples

Bubble sort, example array 5, 3, 8, 4, 2 (expected sorted: 2, 3, 4, 5, 8)

Pass 1 trace: - Compare 5 and 3: 5 > 3, swap → 3, 5, 8, 4, 2. - Compare 5 and 8: 5 is not greater than 8, no swap. - Compare 8 and 4: 8 > 4, swap → 3, 5, 4, 8, 2. - Compare 8 and 2: 8 > 2, swap → 3, 5, 4, 2, 8. - After pass 1, the largest element 8 has reached its correct position.

Pass 2: the last element is not compared again; after pass 2 the next largest element reaches its position (the instructor says "five will reach to this position" — see flagged items).

Bubble sort, second example with 3 elements: 6, 4, 1

  • i = 0, j = 0: compare a[0] and a[1] (6 and 4): swap → 4, 6, 1.
  • i = 1, j = 0: compare a[0] and a[1] (4 and 1): swap → 1, 4, 6.
  • i = 1, j = 1: condition false (already sorted); the swap count is incremented along the way.

Closest pair example

4 points (the transcript mentions coordinates like (−5, 4) and (−5, −4) as an illustration of points on either side of a division); the algorithm computes left minimum, right minimum, then cross-pair minimum, and takes the overall minimum.

Neural network / CNN demos

  • Two-point classification demo: after resetting the window, the points are "perfectly classified," and the classification happens around "like a 500 or 400 box" (as spoken).
  • Digit classification demo: drawing an 8 is properly classified as an 8; layer-by-layer edge detection described (first layer edges, second layer edges and below edges, third layer upper edges, inner size reduced, classified as 8).
  • Sneaker example: instead of a shirt, a sneaker image is taken, to see how the machine learns whether it is a sneaker or not and how it calculates edgewise, starting from the bare minimum ("yanliko" version — [unclear], likely a reference to Yann LeCun).
  • Convolution walkthrough: the image is converted to zeros and ones (0 = black, 1 = white); a filter (values around 0.6, 0.7, 0.76) is applied and the output is zero; then the process moves to the next part, calculating the upper edge and then the below edge; selecting a top-edge filter shows the red top part; selecting a right filter shows the red on the very right. (Filter values mentioned: "0 6 0.6 0.7 0.76", then "0.7 0.7", and later "2.1 2.0 something value stuff" — garbled in the transcript.)

Questions asked in class

  • Q (student): The class challenge shows no sample test cases — was that intentional or is something missing? The same for the second query, "minimum distance between two points." A (instructor): Checked the challenge page; test cases are there. The issue appears when running the code (not submitting). After the instructor's fix, submitting should show input 00 as the first test case and it will be checked. The instructor apologized for the issue (mentioning AWS storage, region us-east-1) and asked students to resubmit.
  • Q (student): "There has been a mistake, I missed out one... test five, so can I retake it?" A (instructor): It's already open; you can submit it.
  • Q (student): Not able to understand / please explain. A (instructor): You can run it yourself and understand; the code was already shown.
  • Q (Jishnu Roy Chowdhury): Thanks for the details; says he feels he needs to learn a lot and learned a lot today. Asks: in the algorithm you were solving and writing code for, what was your approach? I was trying to solve it differently — assume there is a point where all the equidistance are; equidistance is not equal to me; it's possible that's a wrong approach, so it's not equidistance, maybe any distance; the point may exist in minus minus the kind of very long. A (instructor): "that's a very, okay, let me go through, again." (The instructor then says he is stopping the recording because he doesn't know this, and the class ends.)

Key takeaways

  • Bubble sort compares adjacent elements and swaps when the left is greater than the right.
  • It is called "bubble" sort because the largest unsorted element bubbles up to the end each pass.
  • After pass 1, the largest element is in its correct (last) position; the array is not guaranteed sorted.
  • The better version uses inner loop bound n - i - 1 so sorted tail elements are not re-compared.
  • n - 1 in the loop avoids array-out-of-index errors from comparing with the next element.
  • num_swaps starts at 0 and is incremented by 1 after each swap.
  • Closest pair asks for the minimum Euclidean distance between two distinct points.
  • Euclidean distance = sqrt((x2 - x1)^2 + (y2 - y1)^2).
  • Divide-and-conquer for closest pair: left minimum, right minimum, then cross-pair minimum; the overall minimum wins.
  • In the CNN demo, filters detect edges (top-edge filter → top edge; right-edge filter → right side) and max pooling reduces image size.
  • Padding adds back so the image is not cut down after reduction.
  • Image generation traces to physics: diffusion, Brownian motion, and energy-based models.

Exam-style questions

  • Trace bubble sort on 5, 3, 8, 4, 2 for pass 1. Show each comparison and swap; state the array after pass 1 and which element is fixed. (Outline: 5>3 swap → 3,5,8,4,2; 5<8 no swap; 8>4 swap → 3,5,4,8,2; 8>2 swap → 3,5,4,2,8; 8 fixed at the end.)
  • Why does the inner loop use n - i - 1? (Outline: after each pass one more element is correctly placed at the end, so it need not be compared again; n - 1 avoids out-of-index comparison with the next element.)
  • Write bubble sort with a swap counter. (Outline: outer loop over passes, inner loop range(n - i - 1), swap via tuple assignment when array[j] > array[j+1], increment num_swaps after each swap.)
  • Define the closest pair of points problem and give the Euclidean distance formula. (Outline: minimum distance between two distinct points among n points given by (x, y); distance = sqrt((x2 - x1)^2 + (y2 - y1)^2).)
  • Outline the divide-and-conquer closest-pair algorithm. (Outline: sort points by coordinates; split into left/right halves; compute left min and right min; check cross pairs across the dividing line, breaking when the vertical difference exceeds d; take the overall minimum.)

Glossary

  • Bubble sort - sorting algorithm that repeatedly compares adjacent elements and swaps them if out of order.
  • Pass - one full sweep of adjacent comparisons.
  • Swap counter (num_swaps) - variable counting how many swaps occur during the sort.
  • Closest pair of points - problem of finding the minimum Euclidean distance between two distinct points among n points.
  • Euclidean distance - sqrt((x2 - x1)^2 + (y2 - y1)^2) between two points.
  • Divide and conquer - split into left/right halves, solve each, combine (here, also check cross pairs).
  • VLM - vision-language model; the vision transformer starts by taking an image section and passing it through transformations.
  • Filter - small matrix used to calculate edges in a CNN.
  • Max pooling - reducing the image size.
  • Padding - adding to the image so it is not cut down after reduction.
  • Energy-based models - models arising from the "image energy" line of work (diffusion, Brownian motion).
  • Knowledge distillation / dropout - techniques attributed to the "godfather of AI."

Follow-ups

  • The reason for sorting the points by coordinates in the closest-pair algorithm: "I'll tell you why I'm sorting" — not explained in this part.
  • The edge case handling in solve for n ≤ 3 is garbled in the transcript ("minimum distance goes to slope of inf 151 or i in a range") — [unclear].
  • The cross-pair loop details (strip construction with lambda, the for j in range(i+1, ...) loop, the break condition, and the final return) are partially garbled — [unclear].
  • The instructor says there is "something for you today" (the AI/DS resources) and that the closest-pair code is "only the two portions... solve it for today"; further material is deferred.
  • The instructor mentions the code "got confused in case we have this seven... seven will compare to five" — [unclear].
  • The LeetCode-style ML site covers topics like loss functions (20 questions), transformers architecture, tokenization, embedding layer, positional encoding, sigmoid, RMSProp, Adam, and Keras (architecture of "a k3 14" — likely Keras, but kept as spoken) — to be explored by students.
  • The student's algorithm-approach question was not answered; the instructor said he would go through it again — to be addressed later.
  • Self-study question flagged: why do we reduce the image size (max pooling), and if the image is cut down, what methods (e.g., padding) add things back — ask GPT.
  • The instructor stopped the recording because he doesn't know "this" — content after that point is not covered.