Quiz 2

607 words
3 min read
Python Week 1: the first filter for runtime behavior
Visual companion
Python
Type and operator map

Python Week 1: the first filter for runtime behavior

View
Revision summary

What this note is really saying

Short form

# 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.

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?
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.