sorts: make algorithms sort any comparable items, not just ints
維護者通常 1 天內回覆
還沒有人認領這個 Issue。
評估
研究方向
選擇 sorts/ 中一個尚未勾選的比較排序演算法,並使用 insertion_sort.py 作為 Comparable/TypeVar 模式的參考實作。將可比較的非整數案例和 TypeError 案例以 doctest 形式新增,並加入 tests/test_sorts.py。完成的標準是:該演算法保留元素型別、拒絕混合的不可比較輸入,且仍由相關測試涵蓋。
由索引模型根據 Issue 內容生成。
描述
Many of our sorts/ implementations are written and tested only against list[int], even though most comparison sorts work for any items that support <. This issue collects the small, well-scoped changes that make them correctly typed and tested for the general case — a good batch of beginner-friendly PRs for Hacktoberfest.
The type hint to use
Comparison sorts need items that are orderable, not Any. Model that with a small Protocol and a TypeVar bound to it:
from typing import Protocol
class Comparable(Protocol):
def __lt__(self, other: object, /) -> bool: ...
def bubble_sort[T: Comparable](collection: list[T]) -> list[T]:
...
list[T] (with T bound to Comparable) is more precise than list[Any]: it says "a list of items that can be compared with each other" and preserves the element type in the return.
Note: counting/radix/bucket/pigeonhole sorts are not comparison sorts — they rely on integer keys. Those should keep their integer-specific hints and are out of scope here.
The tests to add
For each comparison sort, cover a comparable non-int type and the failure mode. Add both a doctest and a case in tests/test_sorts.py:
# succeeds: strings are comparable
assert bubble_sort(["c", "a", "b"]) == ["a", "b", "c"]
# succeeds: floats and ints are comparable
assert bubble_sort([2.5, -1, 0.0]) == [-1, 0.0, 2.5]
# raises: mixing non-comparable types must not silently mis-sort
import pytest
with pytest.raises(TypeError):
bubble_sort([1, "a"]) # '<' not supported between int and str
The TypeError case matters: a sort that "succeeds" on non-comparable input is a correctness bug, so the test should assert the exception rather than a result.
How to contribute
- Pick one comparison sort from
sorts/(comment which one so we don't double up). - Switch
Any→ theComparable/TypeVarpattern above. - Add the succeed + raise cases as doctests and to
tests/test_sorts.py. - Keep it to one algorithm per PR so reviews stay quick.
- Link this issue without closing it: reference it as
Part of #15234orRef #15234in your PR description — notCloses/Fixes/Resolves #15234. A closing keyword makes GitHub auto-close this umbrella issue when your PR merges, even though other checkboxes remain. This issue should stay open until every box is checked.
I'll help review these and update the checklist below. Refs #15081.
Sort algorithms that can sort any comparable items
- adaptive_merge_sort.py
- bead_sort.py -- not a comparison sort
- binary_insertion_sort.py
- bitonic_sort.py -- needs a power-of-two length
- bogo_sort.py
- bubble_sort.py
- bubble_sort_recursive
- bucket_sort.py -- not a comparison sort
- circle_sort.py
- cocktail_shaker_sort.py
- comb_sort.py
- counting_sort.py -- not a comparison sort
- cycle_sort.py
- cyclic_sort.py
- double_sort.py
- dutch_national_flag_sort.py -- expects only 0/1/2
- exchange_sort.py
- external_sort.py
- flash_sort.py -- not a comparison sort
- gnome_sort.py
- heap_sort.py
- insertion_sort.py -- a good reference implementation to study <--
- intro_sort.py
- iterative_merge_sort.py
- kirkpatrick_reisch_sort.py -- not a comparison sort
- merge_insertion_sort.py
- merge_sort.py
- msd_radix_sort.py -- not a comparison sort
- natural_sort.py
- odd_even_sort.py
- odd_even_transposition_parallel.py
- odd_even_transposition_single_threaded.py
- pancake_sort.py
- patience_sort.py
- pigeon_sort.py -- not a comparison sort
- pigeonhole_sort.py -- not a comparison sort
- power_sort.py
- quick_sort.py
- quick_sort_3_partition.py
- radix_sort.py -- not a comparison sort
- recursive_insertion_sort.py
- recursive_mergesort_array.py
- recursive_quick_sort.py
- reversort.py
- reverse_selection.py
- selection_sort.py
- shell_sort.py
- shrink_shell_sort.py
- slowsort.py
- smoothsort.py
- stalin_sort.py -- causes data loss!
- stooge_sort.py
- strand_sort.py
- tim_sort.py
- topological_sort.py -- sorts directed acyclic graphs
- tree_sort.py
- unknown_sort.py
- wiggle_sort.py -- deliberately doesn't fully sort
- 主要語言
- Python
- 星號
- 225k
- 分支
- 51.1k
- 平均合併
- 18 小時 46 分鐘
- 30 天內合併 PR
- 176
環境準備
在瀏覽器裡用你自己的 GitHub 帳號啟動這個專案的開發容器。
- 沒有 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
TheAlgorithms/Python 的其他 Issue
-
bug
難度 2/5 1-3 小時 新手友好度 88/100
TheAlgorithms/Python#15449 · 1 則留言 ·
維護者通常 1 天內回覆
-
bug
難度 3/5 1-2 天 新手友好度 72/100
TheAlgorithms/Python#15469 ·
維護者通常 1 天內回覆
-
TheAlgorithms/python_maintainers: Call for help in preparing for Hacktoberfest 2026可能已有人在做 @AnupKumarPanwar 於 13 天前認領。 未關閉help wanted
TheAlgorithms/Python#15351 · 已指派 2 人 ·
維護者通常 1 天內回覆
-
難度 5/5 一週以上 新手友好度 25/100
TheAlgorithms/Python#15310 · 23 則留言 ·
維護者通常 1 天內回覆
-
hacktoberfest help wanted tracking issue
難度 4/5 3-5 天 新手友好度 42/100
TheAlgorithms/Python#15187 · 8 則留言 ·
維護者通常 1 天內回覆
查看 TheAlgorithms/Python 的全部 Issue
相似的 Issue
-
難度 2/5 1-3 小時 新手友好度 78/100
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 85/100
維護者通常 1 天內回覆
-
approved correction metadata
難度 1/5 1 小時以內 新手友好度 88/100
acl-org/acl-anthology#10133 · 1 則留言 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 78/100
BasedHardware/omi#20084 ·
維護者通常 1 天內回覆
-
bug needs-acceptance wg/evaluation-quality
難度 2/5 1-3 小時 新手友好度 76/100
vllm-project/semantic-router#4424 ·
維護者通常 1 天內回覆