IITP · Semester ISummaries · Quizzes · Assignments

Lecture summary

DAA 13 Sep

13 Sep 20261h 22m2 parts

Source: IITP-big-files\Sem-1\ECS5101-DAA\Lectures\2026-09-13 0000 DAA 13 Sep\summary\notes.md

Overview

This class completes the asymptotic-notation recap (big O, big Omega, big Theta, little o, little omega, plus reflexivity, transitivity, symmetry, and additive/multiplicative properties) and then introduces the divide-and-conquer paradigm through merge sort: the divide/conquer/combine stages, the merge operation, and full worked traces on 8-element and 7-element arrays. It then derives the merge sort recurrence T(n) = 2·T(n/2) + order of n and contrasts it with binary search on a sorted array, whose recurrence is T(n) = T(n/2) + order of one. The class also reviews in-place vs. stable sorting, duplicates in merge sort, and the space–time trade-off, and closes with course logistics (assignment 1, mid-semester, end-semester). It sits at the transition from complexity analysis into algorithm design, with recurrence-solving methods (master's method, recursion tree, substitution) and quick sort explicitly postponed.

Learning outcomes

  • State and distinguish big O, big Omega, big Theta, little o, and little omega, and apply the highest-degree-term rule to claim complexity directly.
  • Use the properties of asymptotic notation (reflexivity, transitivity, symmetry, additive, multiplicative) to simplify complexity expressions.
  • Explain the divide, conquer, and combine stages of divide and conquer and map them onto merge sort.
  • Trace merge sort on a given array, including the division point (n/2 with int/floor/ceiling fixed consistently) and every merge step.
  • Write/describe the merge procedure with left/right subarrays, result list, left and right indices, and the extend step for the leftover subarray.
  • Write the recurrence relations for merge sort and binary search and explain what each term represents.
  • Trace binary search on a sorted array and state its best-case and general complexity.
  • Distinguish in-place vs. stable sorting and reason about duplicates and space–time trade-offs.

Core concepts

Asymptotic notation recap

Big Omega, big Theta, and big O are the three main notations; little o and little omega are the two others. Little o/little omega are used when the ratio of F(n) and G(n) is not equivalent — i.e., when the chosen value is not equivalent to the value specifying the property of an algorithm. When f(n) is less than g(n), we confine to little o; when f(n) is greater than g(n), we say it is little omega. This matters because it is the vocabulary for every complexity claim in the course.

Highest-degree-term rule

To identify big O, big Omega, or big Theta for any function, always identify the highest-degree term with a positive coefficient; then you can directly claim the complexity (e.g., order of n cube, order of n square) without calculating c1, c2, c, n0. This is the fast path used throughout the course.

Properties of asymptotic notation

  • Reflexivity: a function is asymptotically bounded by itself, e.g., f(n) = Theta(f(n)), f(n) = big O(f(n)), f(n) = big Omega(f(n)).
  • Transitivity: if f1 is bounded by f2 and f2 is bounded by f3, then f1 is bounded by f3.
  • Symmetry: if f(n) = Theta(g(n)), then g(n) = Theta(f(n)), because f(n) and g(n) are equivalent when n is very large.
  • Additive property: sequential, independent operations are added by taking the maximum time consumed (the dominating operation / dominating looping operation). Example: if one part imposes n log n and another n square, max(n square, n log n) = n square.
  • Multiplicative property: if there is a dependency between operations, their contributions are multiplied (e.g., nested loops each running n times give n³).

These properties are what let you collapse a messy algorithm analysis into a single term.

Combining an upper and a lower bound

When one function has only an upper bound and another only a lower bound, the two can combine into a strict relation. Example: f(n) = O(n²) and g(n) = Ω(n³). f grows at most quadratically and g at least cubically, so f/g → 0 and f = o(g). This is the one case where the two bounds give a strict relation rather than just O/Omega.

What asymptotic notation provides

Capability to identify computational complexity of two algorithms / help in algorithmic selection; simplification and abstraction; platform independence; prediction of scalability and behavior as input size increases. This is the "why" behind the whole notation unit.

Divide and conquer

If the problem size is very large, divide the problem into subproblems and sub-subproblems until very small size, then conquer (solve the subproblems) and combine (merge the solutions). Three stages: divide (partition the large problem into smaller and smaller pieces), conquer (use a divide and conquer algorithm to solve the subproblem associated with the subset), combine (take the solutions of the subproblems and do a merger). This is the design paradigm that merge sort and binary search instantiate.

Merge sort

A famous divide and conquer problem. The division strategy divides until a single-element level is reached; then recursively sort (conquer/combine) and perform the combination (merge) operation. Steps: recursively divide the array into one-element composition; merge two elements and sort the subset; repeat until the solution is reached. Division is easy; the merge is the important and critical part. On 8 elements there are 7 merge operations — one per internal node of the recursion tree, i.e., n − 1 merges for n elements. The recursion depth for n elements is log₂ n (6 for 64 elements). If the input is already sorted, the complexity is still O(n log n): it splits and merges everything, so the best case equals the worst case — the big contrast with insertion sort, which is O(n) on sorted input, and the reason hybrid sorts (Timsort) use insertion sort on small runs.

Merge operation

Two already-sorted subsequences are merged: compare the first element of one with the first element of the other; whichever is smaller is pushed into the final/resultant array S and eliminated from its subsequence; two pointers (i and j) move toward the end comparing one-by-one. When one subarray is exhausted, the remaining elements of the other are extended into the result without comparison (they are already sorted). This is the critical operation that determines the order-of-n combine cost. On a tie the left element must be taken; taking the right one reverses the relative order of equal keys and destroys stability.

Recurrence relation for merge sort

A representation of how an algorithm behaves: dividing n into 2 subproblems of n/2 and taking order of n for combining gives T(n) = 2·T(n/2) + order of n. Dividing takes order of one time because nothing is compared — we are just calculating n/2, n/2, n/2 and then dividing. The recursive calls occur until the sublist contains one element; this takes log of n. The merge takes order of n because we compare n/2 elements with n/2 elements, so the maximum number of comparisons could be n. Directly determining that it follows log of n is very difficult, so the recurrence is solved by master theorem (or recursion tree / guessing / substitution) to get n log n.

Given a sorted array (ascending order) and a key value, identify the middle, compare with the key, and determine the direction — if the middle element is greater than the key, search left; if the middle element is less than the key, search right. Half of the array remains unusable once the actual direction of movement is known; each step reduces the search space, which is the beauty of the algorithm. Indexing can start at 1 or 0; for the middle you can take n/2 ceiling, n/2 floor, or n/2 int — totally up to you. The worst-case number of comparisons is ⌈log₂ n⌉ + 1 in the usual midpoint form: 10 for 1024 elements, and 10 for 1023 elements (2¹⁰ = 1024 > 1023, so ten halvings suffice). In a sorted array of n elements there are n + 1 possible unsuccessful searches (the n + 1 gaps). Binary search cannot be applied to a sorted linked list with the same Θ(log n) run time: indexing into the middle of a linked list costs O(n), and binary search needs O(1) random access; a balanced search tree or skip list is needed to get O(log n) on linked structures. With many duplicates in an ascending array, standard binary search lands on some duplicate; to find the first occurrence, continue searching the left half after finding a match.

In-place vs. stable; duplicates; space–time trade-off

Insertion sort is in-place; merge sort is not in-place — "not in place means that the result will be stored into another array." Both are stable: the order of elements in the resultant array remains the same. With duplicates, the algorithm goes as it is and you will get the result; when comparing, "20 less than 20" fails, so that concept can be applied. On space: preference is always given to time; if there is a space constraint, follow "plan B," otherwise stay confined to the time complexity.

Formulas and maths

  • (\text{middle} = \lfloor \text{length of array} / 2 \rfloor) — middle index via integer division; for 7 elements, 7/2 = 3. Floor, ceiling, or int may be used, but the choice must be fixed and consistent throughout the problem.
  • (n/2 = 4) for an 8-element array — the division point gives 4 elements in one part and 4 in the other.
  • (7/2 = 3) (int) — gives 3 elements in one part and 4 in the other.
  • (T(n) = 2 \cdot T(n/2) + \Theta(n)) — merge sort recurrence; the 2 counts the two subproblems of size n/2, and the (\Theta(n)) is the merge/combine cost. Used to derive the n log n complexity via the master theorem.
  • (T(n) = T(n/2) + \Theta(1)) — binary search recurrence; one comparison with the middle element per step. Used to derive the log n complexity.
  • Binary search complexity: (\log n) — because the array is already sorted and the search space halves each step.
  • Binary search best case: (\Theta(1)) — just one comparison; (\log n) applies otherwise.
  • Binary search worst case: (\lceil \log_2 n \rceil + 1) comparisons — 10 for 1024 elements and 10 for 1023 elements (2¹⁰ = 1024 > 1023).
  • Unsuccessful searches in a sorted array of n elements: (n + 1) — one per gap.
  • Binary search cases ((k) = key, (a) = array):
  • If (k = a[n/2]), we are done.
  • If (k < a[n/2]), search (k) between (a[0]) and (a[n/2 - 1]).
  • If (k > a[n/2]), search (k) between (a[n/2 + 1]) and (a[n - 1]).
  • Repeat until either (k) is found or no element exists to search.
  • Merge sort complexity: (n \log n) — stated, to be obtained by solving the recurrence via the master theorem.
  • Merge sort recursion depth: (\log_2 n) — 6 for 64 elements.
  • Merge operations for n elements: (n - 1) — 7 for 8 elements.
  • Master theorem, case 1 example: (T(n) = 4T(n/2) + n) → (a = 4), (b = 2), (n^{\log_2 4} = n^2) dominates (f(n) = n), so (T(n) = \Theta(n^2)).
  • Matrix multiplication (multiplicative property illustration): (n \times n \times n = n^3) — nested loops each running n times multiply.
  • Additive property: (\max(n^2, n\log n) = n^2) — the dominating term wins.

Worked examples

Merge sort on 8 elements: 85, 24, 63, 45, 17, 31, 96, 50 - n/2 = 4, so division point after the 4th element: left part 85, 24, 63, 45; right part 17, 31, 96, 50. - Left portion solved first: 85, 24, 63, 45 divides into (85, 24) and (63, 45). - Merge 85 (S1) and 24 (S2): compare first elements, store 24 then 85 → result 24, 85. - Merge 63 (S1) and 45 (S2): result 45, 63. - Merge S1 = 24, 85 with S2 = 45, 63: 24, then 45, then 63, then 85 → result 24, 45, 63, 85. - Right portion: 17, 31, 96, 50 divides into (17, 31) and (96, 50). - Merge 17 and 31 → 17, 31. Merge 96 and 50 → 50, 96. - Merge S1 = 17, 31 with S2 = 50, 96 → 17, 31, 50, 96. - Final merge: S1 = 24, 45, 63, 85 and S2 = 17, 31, 50, 96 → result 17, 24, 31, 45, 50, 63, 85, 96. - Total merge operations: 7 (= n − 1).

Merge sort on 7 elements: 38, 27, 43, 3, 9, 82, 10 - n = 7; 7/2 int = 3, so parts are 38, 27, 43 and 3, 9, 82, 10. - First part divides into 38 and (27, 43); 27 and 43 merge → 27, 43. - Merge S1 = 38 with S2 = 27, 43 → 27, 38, 43. - Second part: 3, 9, 82, 10 divides into (3, 9) and (82, 10); merge 3 and 9 → 3, 9; merge 82 and 10 → 10, 82. - Merge S1 = 3, 9 with S2 = 10, 82 → 3, 9, 10, 82. - Final merge: S1 = 27, 38, 43 and S2 = 3, 9, 10, 82 → 3, 9, 10, 27, 38, 43, 82.

Merge procedure example (instructor's illustration) - Left subarray: 17, 19, 23, 45 (indices 0 1 2 3); right subarray: 16, 24, 49, 95; result initially empty; left index = right index = 0. - Compare left[0] = 17 with right[0] = 16: 17 < 16 fails, so else part appends right[0] = 16 to result; right index becomes 1. - Next: left index 0, right index 1; compare left[0] = 17 with right[1] = 24: condition fulfills, append left[0] = 17; left index becomes 1. - Next: compare left[1] = 19 with right[1] = 24: 19 comes; then left index 2, right index 2; compare 23 with 24: 23 comes; then left index 3, right index 2; compare 45 with 24: 24 comes; then compare 45 with 49: 45 comes; then left subarray is exhausted, so the remaining right elements are extended into the result. - (Note: the transcript's narration of this example contains some garbled numbers, e.g., "70" and "24 19"; the above follows the stated comparison logic.)

Extend illustration: If S1 has 0 entries and S2 still has elements (e.g., 74, 75, 73) while S has 1, 7, 9 already sorted, the remaining subarray is directly cut down and added without comparison. This can be the left or right subarray; the remaining elements are already sorted, so they are extended into the final result, and in that case the comparison will be lesser.

Merge sort structure (recurrence intuition): a problem of 4 divides into 2 and 2, and combining takes n; a problem of 8 gives 2 problems of 4, 4, and combining requires order of n time.

Binary search, first example: sorted array 1, 7, 9, 24, 29, 9, 31, 35, 45 (8 elements listed; the instructor notes you can do a 9th element). Key = 45. Apply n/2 → reach index 4, value 24. 45 > 24, so the left portion is irrelevant. Remaining 5 elements → 5/2 → take the second entry, 31. 31 compared with 45 → that portion is done. Remaining 3 elements → get 35; 35 compared with 45 → that portion is irrelevant. Come to 45 and 51; 45 compared with 45 → declare the result.

Binary search, second example: values 6, 13, 14, 25, 33, 43, 51, 53, 64, 72, 84, 93, 95, 96, 97; low index 0, high index 14. Key = 33. Using ceiling of n/2, reach index 7 (value 53). 33 vs 53 → it will never lie in that direction (array sorted), so consider only that portion. Then 0 to 6 → reach index with value 25; compare 33 with it → that portion is inefficient. Then get 43 → that portion is irrelevant. Finally 33 is both low and high index → get the result.

Searching 20: using ceiling of n/2, reach 100; 20 < 100 so the value may lie on the left, not the right. Then middle element is 20; compare 20 with 20 → declare the result. Found in just two comparisons: one with 100 and one with 20.

Master theorem on T(n) = 4T(n/2) + n: a = 4, b = 2, so n^(log₂ 4) = n²; the f(n) = n term is smaller, so case 1 applies and T(n) = Θ(n²).

Questions asked in class

Q (Subhashish): If the biggest term has a negative coefficient, do we ignore it or not take it? A: That is not solvable by asymptotic analysis; the assumption is that the highest order term should have a positive coefficient, then only asymptotic analysis can be applied.

Q: The last part of the merge sort explanation was not properly visible (screen may have frozen); can you explain the last portion after division? A: Division and revision were clear; the instructor continued with the merge portion, noting the PPT is shared and the merge operation will be explicitly demonstrated.

Q: How is the middle calculated? For an odd number, 7 by 2 is 3 — is that what we use? A: Yes; if middle is declared as an int variable you get 3. You are free to use floor, ceiling, or int, but once declared it should be specific and the same throughout the problem.

Q: What is result.extend doing? A: append adds one element; extend adds extra entries/spaces to the list (specific to Python). Since the list is dynamic, after appending one element you extend it for further entries. More importantly, when one subarray has no elements left, the remaining elements of the other subarray are automatically appended without comparing (could be left or right portion), since they are already sorted; in that case the comparison will be lesser.

Q: Can you explain the procedure of merge with an example? A: The instructor worked through the left = 17, 19, 23, 45 / right = 16, 24, 49, 95 example (see Worked examples).

Q: With one million/ten million items, merge sort requires additional space (twice the amount) to sort the array — is this the most efficient from industry standards, or is there something better? A: Better than this is quick sort, the quickest algorithm; it will be covered in the next class. It does not use extra space and utilizes the same array for sorting — that is why it is called quick sort.

Ganesh (merge sort in-place/stable): Confusion over the earlier statement that merge sort is in-place and also stable. A: Merge sort is not in-place; not in-place means the result is stored into another array. Insertion sort is in-place. Both are stable — the order of elements in the resultant array remains the same.

Ganesh (duplicates in merge sort): How to proceed when duplicate elements

Key takeaways

  • f(n) = O(n²) together with g(n) = Ω(n³) forces f = o(g): an upper bound of one function and a lower bound of another can combine into a strict relation.
  • Master theorem case 1: T(n) = 4T(n/2) + n gives Θ(n²), since n^(log₂ 4) = n² dominates n.
  • Merge sort on n elements performs n − 1 merges (7 for 8 elements) and has recursion depth log₂ n (6 for 64 elements).
  • Merge sort is O(n log n) even on already-sorted input — best case equals worst case; insertion sort is O(n) there, which is why hybrid sorts use insertion sort on small runs.
  • Binary search worst case is ⌈log₂ n⌉ + 1 comparisons: 10 for 1024 elements and 10 for 1023 elements.
  • A sorted array of n elements admits n + 1 unsuccessful searches.
  • Binary search needs O(1) random access, so it does not run in Θ(log n) on a sorted linked list (middle access costs O(n)).
  • To find the first occurrence among duplicates, keep searching the left half after a match.
  • Stability in merge sort depends on taking the left element on a tie; taking the right one reverses equal keys.
  • Insertion sort stays stable when it shifts only while A[j] > key (not ≥); using ≥ lets an element jump ahead of equals and breaks stability.
  • Insertion sort is Θ(n²) worst case and Θ(n) best case, never Θ(n³); on an array with k out-of-place elements it costs Θ(n + I), where I is the inversion count (0 to n(n−1)/2).
  • Asymptotics ignore constants: Θ(n log n) does not mean exactly twice the time when n doubles.
  • Termination alone does not prove correctness; total correctness = partial correctness + termination, and a recursive proof needs a correct base case.
  • Loop-invariant proof steps: initialisation, maintenance, termination — the induction base, step and conclusion.
  • Closest pair of points with an initial sort and strip processing runs in Θ(n log n): T(n) = 2T(n/2) + Θ(n), at most 7 neighbours compared per point.
  • Maximum contiguous subarray: with all-negative values the non-empty answer is the largest single element (e.g., −1 for [−5,−2,−8,−1,−6]); divide and conquer must also consider the crossing subarray, and Kadane's Θ(n) beats brute force Θ(n³) (Θ(n²) with prefix sums).
  • Quick sort: average O(n log n), Θ(n²) with first-element pivot on a sorted array, worst case on both ascending and descending input; median-of-medians pivot guarantees O(n log n), true-median pivot gives T(n) = 2T(n/2) + n, median-of-three mainly reduces the likelihood of the worst case, and quick sort is unstable because equal elements may swap during long-distance partitioning swaps.

Glossary

  • Big O / big Omega / big Theta: upper, lower, and tight asymptotic bounds.
  • Little o / little omega: strict asymptotic relations used when the ratio of f(n) to g(n) is not equivalent.
  • Reflexivity / transitivity / symmetry: properties letting a function bound itself, chain bounds, and swap sides of a Theta relation.
  • Additive property: sequential independent work adds by taking the maximum (dominating) term.
  • Multiplicative property: dependent work (nested loops) multiplies.
  • Divide and conquer: divide into subproblems, conquer each, combine the solutions.
  • Merge sort: divide-and-conquer sort with T(n) = 2T(n/2) + Θ(n) → n log n; not in-place, stable; n − 1 merges, depth log₂ n.
  • Merge: combine two sorted subsequences with two pointers, extending the leftover subarray without comparison; take the left element on ties to keep stability.
  • Binary search: halve a sorted array by comparing with the middle; Θ(log n), best case Θ(1), worst case ⌈log₂ n⌉ + 1 comparisons; needs O(1) random access.
  • In-place: sorts within the same array (insertion sort); merge sort is not in-place.
  • Stable: equal elements keep their relative order (insertion sort and merge sort are stable).
  • Master theorem: method for solving recurrences like T(n) = aT(n/b) + f(n); case 1 compares f(n) with n^(log_b a).
  • Loop invariant: property proved by initialisation, maintenance, and termination.
  • Total correctness: partial correctness plus termination.
  • Kadane's algorithm: Θ(n) maximum-subarray scan; brute force is Θ(n³) or Θ(n²) with prefix sums.
  • Median-of-medians (BFPRT): deterministic linear-time pivot selection giving quick sort a guaranteed O(n log n) worst case.