Skip to content

Commit 02eadee

Browse files
committed
Add heapselect strategy
1 parent 42cd9b3 commit 02eadee

File tree

4 files changed

+381
-131
lines changed

4 files changed

+381
-131
lines changed

benchmark.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python3
22
"""
3-
Benchmark comparisons for three methods to obtain the K smallest items from a list,
3+
Benchmark comparisons for four methods to obtain the K smallest items from a list,
44
for various values of K with different list sizes N (varying from 1,000 to 1,000,000).
55
66
For each method and each chosen K (as a percentage of N), the test is run 5 times
@@ -10,9 +10,10 @@
1010
1. Using built‐in sort: sort the list and slice the first K elements.
1111
2. Using heapq.nsmallest: use the heap‐based algorithm.
1212
3. Using quickselect: partition the list with selectlib.quickselect and slice the first K elements.
13+
4. Using heapselect: partition the list with selectlib.heapselect and slice the first K elements.
1314
1415
The benchmark results are then plotted as grouped bar charts (one per N value) in a vertical stack.
15-
Note: The percentages for K are now 0.1%, 1%, 10%, and 25% of N.
16+
Note: The percentages for K are now 0.2%, 1%, 10%, and 25% of N.
1617
"""
1718

1819
import random
@@ -40,17 +41,28 @@ def bench_quickselect(values, K):
4041
is in the correct sorted position; then sort and return the first K elements.
4142
"""
4243
lst = values.copy()
43-
# Partition in-place so that the element at index (K-1) is in the correct position.
44+
# Partition in-place so that the element at index (K-1) is in the correct position
4445
selectlib.quickselect(lst, K - 1)
4546
result = lst[:K]
4647
result.sort()
4748
return result
4849

50+
def bench_heapselect(values, K):
51+
"""
52+
Use selectlib.heapselect on a copy of the list to partition it so that the element at index K-1
53+
is in the correct sorted position; then sort and return the first K elements.
54+
"""
55+
lst = values.copy()
56+
# Partition in-place so that the element at index (K-1) is in the correct position.
57+
selectlib.heapselect(lst, K - 1)
58+
return lst[:K]
59+
4960
# List of methods to benchmark
5061
methods = {
5162
"sort": bench_sort,
5263
"heapq.nsmallest": bench_heapq,
5364
"quickselect": bench_quickselect,
65+
"heapselect": bench_heapselect,
5466
}
5567

5668
def run_benchmarks():
@@ -111,11 +123,13 @@ def plot_results(overall_results):
111123
"sort": -bar_width,
112124
"heapq.nsmallest": 0,
113125
"quickselect": bar_width,
126+
"heapselect": bar_width*2,
114127
}
115128
method_colors = {
116129
"sort": '#1f77b4',
117130
"heapq.nsmallest": '#ff7f0e',
118-
"quickselect": '#2ca02c'
131+
"quickselect": '#2ca02c',
132+
"heapselect": '#d62728'
119133
}
120134

121135
# Sort the overall_results by N for proper ordering (smallest to largest)

plot.png

13.2 KB
Loading

0 commit comments

Comments
 (0)