Skip to content

Commit cc45646

Browse files
Docs: Update sorting algorithm references for Python 3.11+ Powersort
Python 3.11 switched from Timsort to Powersort (an improved merge policy with the same complexity characteristics). Co-Authored-By: Amp <amp@ampcode.com>
1 parent aaf158c commit cc45646

7 files changed

Lines changed: 19 additions & 17 deletions

File tree

DOCUMENTATION_REVIEW_PROMPT.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ For each operation listed:
3131
# ❌ WRONG: "list.append() is O(1)" without context
3232
# ✅ CORRECT: "list.append() is O(1) amortized (dynamic array resizing)"
3333

34-
# ❌ WRONG: "sorted() is O(n log n)" without mentioning Timsort
35-
# ✅ CORRECT: "sorted() is O(n log n) (Timsort), O(n) best case (already sorted)"
34+
# ❌ WRONG: "sorted() is O(n log n)" without algorithm details
35+
# ✅ CORRECT: "sorted() is O(n log n) (Timsort/Powersort), O(n) best case (already sorted)"
3636
```
3737

3838
### ✅ Code Examples (FOCUSED)
@@ -177,7 +177,7 @@ if 5 in items: # O(n)
177177
"sorted() uses Timsort algorithm"
178178

179179
# AFTER
180-
"sorted() uses Timsort, O(n log n) average and worst case, O(n) best case (already sorted), O(n) space"
180+
"sorted() uses Timsort (≤3.10) or Powersort (3.11+), O(n log n) average and worst case, O(n) best case (already sorted), O(n) space"
181181

182182
# BEFORE (too generic)
183183
```python

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ Example: Add: List complexity documentation
175175
| `pop()` | O(1) | Last element |
176176
| `pop(0)` | O(n) | First element |
177177
| `in` | O(n) | Linear search |
178-
| `sort()` | O(n log n) | Timsort |
178+
| `sort()` | O(n log n) | Timsort/Powersort |
179179

180180
**Pro tip:** Use `deque.appendleft()` for O(1) prepend instead of `list.insert(0)`.
181181

docs/builtins/list.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ The `list` type is a mutable, ordered sequence. It's implemented as a dynamic ar
1818
| `clear()` | O(n) | Deallocate memory |
1919
| `index(x)` | O(n) | Linear search |
2020
| `count(x)` | O(n) | Linear scan |
21-
| `sort()` | O(n log n) | Timsort algorithm |
21+
| `sort()` | O(n log n) | Timsort/Powersort |
2222
| `reverse()` | O(n) | In-place reversal |
2323
| `copy()` | O(n) | Shallow copy |
2424
| `extend(iterable)` | O(k) | k = length of iterable |

docs/builtins/sorted.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ The `sorted()` function returns a new sorted list from the items in an iterable.
66

77
| Case | Time | Space | Notes |
88
|------|------|-------|-------|
9-
| Basic sorting | O(n log n) | O(n) | Timsort algorithm |
9+
| Basic sorting | O(n log n) | O(n) | Timsort/Powersort |
1010
| With key function | O(n log n + n*k) | O(n) | k = key function time; key computed once per element |
1111
| Reverse sorting | O(n log n) | O(n) | No extra overhead |
12-
| Already sorted | O(n) | O(n) | Best case for Timsort |
12+
| Already sorted | O(n) | O(n) | Best case |
1313

1414
## Basic Usage
1515

1616
### Simple Sorting
1717

1818
```python
19-
# O(n log n) - uses Timsort algorithm
19+
# O(n log n) - Timsort (≤3.10) or Powersort (3.11+)
2020
numbers = [3, 1, 4, 1, 5, 9, 2, 6]
2121
result = sorted(numbers)
2222
# [1, 1, 2, 3, 4, 5, 6, 9]
@@ -102,16 +102,19 @@ result = sorted(coords, key=lambda c: c[1])
102102
# [(3, 2), (1, 5), (2, 8)]
103103
```
104104

105-
## Timsort Algorithm
105+
## Sorting Algorithm
106106

107107
### How It Works
108108

109109
```
110-
Timsort is a hybrid algorithm combining merge sort and insertion sort:
110+
Python uses Timsort (Python 2.3-3.10) or Powersort (Python 3.11+).
111+
Both are hybrid algorithms combining merge sort and insertion sort:
111112
1. Divide array into small chunks (runs) - ~32-64 elements
112113
2. Sort each run with insertion sort - O(k²) per run
113114
3. Merge runs together - O(n log n) overall
114115
4. Already sorted data: O(n) - detects and uses it
116+
117+
Powersort uses an improved merge policy but has the same complexity.
115118
```
116119

117120
### Performance Characteristics
@@ -147,7 +150,7 @@ original = [3, 1, 4, 1, 5]
147150
original.sort() # [1, 1, 3, 4, 5]
148151
# original modified
149152

150-
# Both use Timsort, same complexity but sorted() makes copy
153+
# Both use same algorithm, same complexity but sorted() makes copy
151154
```
152155

153156
### Expensive Key Functions
@@ -325,6 +328,5 @@ result = sorted(numbers) # Nearly O(n)
325328

326329
## Version Notes
327330

328-
- **Python 2.x**: Uses Timsort since 2.3
329-
- **Python 3.x**: Same Timsort implementation
330-
- **Python 3.8+**: No changes to complexity, consistent performance
331+
- **Python 2.3-3.10**: Uses Timsort algorithm
332+
- **Python 3.11+**: Uses Powersort (improved merge policy, same complexity)

docs/implementations/cpython.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ gc.collect() # O(n) where n = tracked objects
9999
| `insert(0)` | O(n) - no special optimization |
100100
| `pop()` | O(1) |
101101
| `pop(0)` | O(n) |
102-
| `sort()` | O(n log n) using Timsort |
102+
| `sort()` | O(n log n) using Timsort (≤3.10) or Powersort (3.11+) |
103103

104104
### Dict Operations
105105

docs/versions/py311.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ lst.extend([2,3]) # O(k) for k items
166166
lst.insert(0, 0) # O(n) shift all
167167
lst.pop() # O(1)
168168
lst.pop(0) # O(n) shift all
169-
lst.sort() # O(n log n) Timsort
169+
lst.sort() # O(n log n) Powersort
170170
lst[i] # O(1)
171171
1 in lst # O(n) search
172172
```

docs/versions/py313.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ lst = list(range(1000000))
9898
# append: O(1)* - same as 3.12
9999
lst.append(999999)
100100

101-
# sort: O(n log n) - Timsort unchanged
101+
# sort: O(n log n) - Powersort (since 3.11)
102102
lst.sort()
103103
```
104104

0 commit comments

Comments
 (0)