Quiz 2
Registry Synced

Computational Thinking · Week 5 — Lists & insertion sort

1127 words
6 min read
2026-08-16

Reading compass

Now · Week map

Week 5 — lists & insertion sort

Quiz 2 scope: Weeks 1–8 per IITM May 2026 foundation courses. Source baseline: IITM BS admissions important-dates calendar · May 2026 cycle. Times on assessments are operational conventions — verify hall ticket.

Week map

List ops → traverse → insertion sort shifts → sorted invariant

Classify → Represent → Execute → Trap-check

  • Recognize: Ask: What is sorted after outer index i in insertion sort?
  • Procedure: Build new list or update in place per spec. Keep index valid: 0 to len-1.
  • Variations / traps: Watch for: Shift direction wrong in insertion sort.

Formula chain (compressed)

list insert/delete → traversal index → insertion sort trace.
  1. List indexL[i] read/write — 0-based positions
  2. Insertshift right, place key — insertion sort inner loop
  3. Sorted prefixL[0..i-1] sorted — insertion sort invariant
  4. Compare-shiftwhile L[j-1] > key: shift — trace swaps
  5. Stable sortequal keys keep order — insertion sort property

Deep study

Computational Thinking · Week 5 — Lists and insertion sort

Deep study for Quiz 2 week 5. Lists store ordered sequences; insertion sort builds a sorted prefix by shifting larger neighbors right.

Week map

List operations → 0-based indexing → traversal → insertion sort outer loop → inner shift → sorted invariant.

List notation (algorithmic view)

  • List → ordered sequence of items at positions 0,1,,n10, 1, \ldots, n-1.
  • len(L) → length nn.
  • L[i] → item at index ii → valid when 0i<n0 \leq i < n.
  • Append → add at end; insert → open slot at index, shift right.
Mini-example: L=[5,2,8,1]L = [5, 2, 8, 1]. L[0]=5L[0] = 5, L[1]=1L[-1] = 1 (last), len(L)=4\text{len}(L) = 4.

Traversal

Visit each position once:
text
for i from 0 to len(L)-1:
    process L[i]
Or while-index style with counter increment.

Insertion sort notation

  • Outer index ii → next element to insert into sorted prefix L[0..i1]L[0..i-1].
  • Key → value at L[i]L[i] to place correctly.
  • Inner index jj → walk backward while L[j]>keyL[j] > \text{key}, shift L[j]L[j] right to L[j+1]L[j+1].
  • Invariant: after each outer step, L[0..i]L[0..i] is sorted.
Mini-trace on [3,1,4,2][3, 1, 4, 2]:
iikeyafter shiftssorted prefix
11[1, 3, 4, 2]first 2
24no shiftfirst 3
32[1, 2, 3, 4]all 4

Pattern families

Easy — Traverse and transform

Visit each index; compute sum, count, or build new list. Keep indices in range 00 to len1\text{len}-1.

Medium — One insertion sort pass

Given ii and array state, trace inner shifts. Place key at final hole j+1j+1.

Hard — Full sort trace

Table columns: ii, key, array after each outer iteration. Count shifts. Already-sorted input: minimal inner work but outer still runs n1n-1 times.

Worked mini-examples

Example 1 — Index access.
L=[10,20,30]L = [10, 20, 30]. L[1]=20L[1] = 20. Insert 15 at index 1 → [10,15,20,30][10, 15, 20, 30].
Example 2 — One shift step.
Array [2,5,5,7][2, 5, 5, 7], key =4= 4 at position 2. Compare L[1]=5>4L[1]=5 > 4: shift → [2,5,5,7][2, 5, 5, 7] then [2,5,5,7][2, 5, 5, 7]... walk jj from 1: L[1]=5>4L[1]=5>4 shift to index 2 → [2,5,5,7][2, 5, 5, 7]; L[0]=24L[0]=2 \not> 4; place key at j+1=1j+1 = 1[2,4,5,7][2, 4, 5, 7].
Example 3 — Sorted input.
[1,2,3][1, 2, 3]: each key already in place — zero shifts, still n1n-1 outer passes.
Example 4 — Reverse input.
[3,2,1][3, 2, 1]: maximum shifts — roughly 1+2=31 + 2 = 3 shifts for n=3n=3.
Example 5 — Locate after shifts.
Inner loop stops when L[j]keyL[j] \leq \text{key} or j<0j < 0. Write key at L[j+1]L[j+1].

Traps

  • Shift direction wrong — move larger elements right, not left.
  • Off-by-one: outer ii typically starts at 1 (first element trivially sorted).
  • Confusing insert index after multiple shifts.
  • Using ii past len1\text{len}-1.
  • Assuming O(n)O(n) because small nn in trace — algorithm is O(n2)O(n^2) comparisons in worst case.

Diagnostic (try yourself)

  1. List [7,3,9,1][7, 3, 9, 1]. What is index of element 9? What is len\text{len}?
  2. One outer step of insertion sort at i=1i=1 on [4,2,5][4, 2, 5]. Show array after placing key.
  3. How many outer iterations for list of length 5?
  4. After sorting [5,1,4][5, 1, 4], what is the invariant about L[0..i]L[0..i] after each ii?
  5. Why does insertion sort do little work on already-sorted input?

ChatGPT prep archive

Archived import for extra depth — complements the notes above, not official IITM material.

Core concepts

  • List ops: append, insert, length, index access; 0-based indexing.
  • Traversal: visit each position once with for or while index.
  • Insertion sort: for each position, shift larger elements right, insert into sorted prefix.
  • Invariant: items left of index are sorted after each outer step.

Notation & vocabulary

StepAction
outer inext element to place
inner jshift while larger
insertwrite key at hole

Pattern families

Easy — Traverse and transform

Build new list or update in place per spec. Keep index valid: 0 to len-1.

Medium — Insertion sort pass

Take element at i, walk j backward while arr[j] > key, shift arr[j+1]=arr[j], place key at j+1.

Hard — Full sort trace

Table columns: i, key, array after shifts. Sorted prefix grows one element per i. Already-sorted input still runs but minimal shifts.
Drill these on the pattern atlas — filter to week 5.

Traps

  • Shift direction wrong in insertion sort.
  • Using i past len-1.
  • Confusing insert index after shifts.
  • O(n²) trace confusion on small n—still trace mechanically.

Retrieval prompts

  1. What is sorted after outer index i in insertion sort?
  2. How does shift step work?
  3. Difference traverse vs sort?

Practice loop

  1. Read Deep study (if present) or core concepts once.
  2. Recite the formula chain without looking.
  3. Open one easy pattern on the interactive atlas for week 5.
  4. Attempt without solutions; mark studied after an honest try.
  5. Say one trap aloud before closing the tab.
Document outline

Keep your place and jump directly to a heading.

Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.