Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

sorts: make algorithms sort any comparable items, not just ints

オープン 初心者向け
#15,234 コメント 91 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
74/100
issue の種類
機能追加
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python
領域
data

調査の方向性

sorts/ にある未チェックの比較ソートを1つ選び、Comparable/TypeVar パターンのリファレンス実装として insertion_sort.py を使用してください。比較可能な非整数のケースと TypeError のケースを doctest と tests/test_sorts.py に追加してください。完了条件は、1つのアルゴリズムが要素の型を保持し、比較できない型が混在した入力を拒否し、関連するテストで引き続きカバーされていることです。

索引モデルが issue の本文から書いたものです。

説明

enhancement good first issue hacktoberfest tracking 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 → the Comparable/TypeVar pattern 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 #15234 or Ref #15234 in your PR description — not Closes/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
平均マージ
16時間 12分
マージ済み PR(30日)
180

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

TheAlgorithms/Python のほかの issue

TheAlgorithms/Python の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。