607 words
3 min read
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,…,n−1.
len(L)→ length n.L[i]→ item at index i → valid when 0≤i<n.- Append → add at end; insert → open slot at index, shift right.
Mini-example: L=[5,2,8,1]. L[0]=5, L[−1]=1 (last), len(L)=4.
Traversal
Visit each position once:
textfor i from 0 to len(L)-1: process L[i]
Or while-index style with counter increment.
Insertion sort notation
- Outer index i → next element to insert into sorted prefix L[0..i−1].
- Key → value at L[i] to place correctly.
- Inner index j → walk backward while L[j]>key, shift L[j] right to L[j+1].
- Invariant: after each outer step, L[0..i] is sorted.
Mini-trace on [3,1,4,2]:
| i | key | after shifts | sorted prefix |
|---|---|---|---|
| 1 | 1 | [1, 3, 4, 2] | first 2 |
| 2 | 4 | no shift | first 3 |
| 3 | 2 | [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 0 to len−1.
Medium — One insertion sort pass
Given i and array state, trace inner shifts. Place key at final hole j+1.
Hard — Full sort trace
Table columns: i, key, array after each outer iteration. Count shifts. Already-sorted input: minimal inner work but outer still runs n−1 times.
Worked mini-examples
Example 1 — Index access.
L=[10,20,30]. L[1]=20. Insert 15 at index 1 → [10,15,20,30].
Example 2 — One shift step.
Array [2,5,5,7], key =4 at position 2. Compare L[1]=5>4: shift → [2,5,5,7] then [2,5,5,7]... walk j from 1: L[1]=5>4 shift to index 2 → [2,5,5,7]; L[0]=2>4; place key at j+1=1 → [2,4,5,7].
Example 3 — Sorted input.
[1,2,3]: each key already in place — zero shifts, still n−1 outer passes.
Example 4 — Reverse input.
[3,2,1]: maximum shifts — roughly 1+2=3 shifts for n=3.
Example 5 — Locate after shifts.
Inner loop stops when L[j]≤key or j<0. Write key at L[j+1].
Traps
- Shift direction wrong — move larger elements right, not left.
- Off-by-one: outer i typically starts at 1 (first element trivially sorted).
- Confusing insert index after multiple shifts.
- Using i past len−1.
- Assuming O(n) because small n in trace — algorithm is O(n2) comparisons in worst case.
Diagnostic (try yourself)
-
List [7,3,9,1]. What is index of element 9? What is len?
-
One outer step of insertion sort at i=1 on [4,2,5]. Show array after placing key.
-
How many outer iterations for list of length 5?
-
After sorting [5,1,4], what is the invariant about L[0..i] after each i?
-
Why does insertion sort do little work on already-sorted input?