32 Commits
Author SHA1 Message Date
Milad Khoshdel a7a298e417 Fix introsort heap fallback ignoring range boundaries (#15435)
* fix: restrict introsort heap fallback to the active range

* test: cover introsort heap fallback range boundaries

* fix: sort introsort heap fallback ranges without copying

* test: cover introsort heap sort range boundaries and default end
2026-09-27 14:46:23 +02:00
KratosandChristian Clauss f59bddb055 fix(sorts): make smoothsort generic over Comparable items (#15441)
* fix(sorts): make smoothsort generic over Comparable items

* The TypeVar is no longer needed.

---------

Co-authored-by: Christian Clauss <cclauss@me.com>
2026-09-26 10:13:40 +02:00
e49caf57d0 types(reverse_selection): constrain items to Comparable (#15410)
* types(reverse_selection): constrain items to Comparable

Bind reverse_selection_sort's element type to a Comparable Protocol so the
signature says "a list of items that can be compared with each other"
instead of a bare list, and keep the element type in the return.

reverse_subarray only swaps elements and never compares them, so its
TypeVar stays unbounded.

Also adds doctests for a comparable non-int type (strings, floats) and for
the failure mode: mixing non-comparable items must raise TypeError rather
than silently mis-sort. The test battery picks the sort up for the shared
cases and for the rejection check.

* updating DIRECTORY.md

---------

Co-authored-by: AuroraAeon <auroraeon@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
Co-authored-by: cclauss <cclauss@users.noreply.github.com>
2026-09-24 22:09:11 +02:00
ModusensusandChristian Clauss 1d64d4cda6 sorts: make quick_sort_3_partition generic over Comparable items (#15431)
* sorts: make quick_sort_3_partition generic over Comparable items

* sorts: rebase quick_sort_3_partition generic change onto upstream rewrite

---------

Co-authored-by: Christian Clauss <cclauss@me.com>
2026-09-24 22:03:33 +02:00
b3f113dc4b types: constrain tim sort items to Comparable (#15432)
Co-authored-by: TayfurYldz <238304586+TayfurYldz@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
2026-09-24 16:26:56 +02:00
Ramvelivelapre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>Christian Clauss
18c82c9d60 Bitonic sort comparable (#15428)
* Make bitonic sort support comparable items

* Add comparable item tests for bitonic sort

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
2026-09-24 16:16:10 +02:00
Fayupable d961217d5f sorts: type patience_sort's Stack for any comparable item (#15427)
Part of #15234.
2026-09-24 16:08:14 +02:00
Fayupable c41bc04f33 sorts: type unknown_sort's merge_sort for any comparable item (#15426)
Part of #15234.
2026-09-24 16:05:09 +02:00
a1fb42f058 types(iterative_merge_sort): constrain items to Comparable (#15413)
Bind merge and iter_merge_sort's element type to a Comparable Protocol so
the signatures say "a list of items that can be compared with each other"
instead of a bare list, and keep the element type in the return. result is
annotated list[T] so the generic survives the local binding.

iter_merge_sort was already in the shared SORTS battery but was missing from
test_sort_rejects_non_comparable_items, so its checklist box was only half
done; this completes it.

Also adds a doctest for the failure mode: mixing non-comparable items must
raise TypeError rather than silently mis-sort. The 'cba' doctest becomes
list('cba') so it matches the list[T] signature now on the function -- the
runtime behaviour is unchanged, since iter_merge_sort copies its input with
list() either way.

Co-authored-by: AuroraAeon <auroraeon@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
2026-09-24 16:02:45 +02:00
ModusensusandChristian Clauss 16fd0d5519 sorts: make merge_insertion_sort generic over Comparable items (#15417)
Co-authored-by: Christian Clauss <cclauss@me.com>
2026-09-24 15:58:39 +02:00
FelixandChristian Clauss c4d6a2a3c3 sorts: type intro sort for comparable items (#15403)
* Refactor sorting functions to use generics

* test: cover intro sort comparable items

* Placate ruff rule PT007

* test: include intro sort in shared sort suite

* Update test_sorts.py

* Delete tests/test_intro_sort.py

---------

Co-authored-by: Christian Clauss <cclauss@me.com>
2026-09-24 15:55:56 +02:00
60cea20f85 feat(sorts): make odd-even transposition generic over comparable items (#15405)
Part of #15234

- Replace list[Any] with a Comparable-bounded TypeVar so
  odd_even_transposition sorts any mutually comparable items,
  not just ints
- Add str/float/int success doctests plus a TypeError rejection
  doctest for non-comparable mixed input
- Assert in-place behavior via `result is values` doctest
- Register the sort in the shared battery and in
  test_sort_rejects_non_comparable_items

Co-authored-by: ayushman-it <253447966+ayushman-it@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
2026-09-23 08:46:44 +02:00
Columbus Labs cce4d26388 fix: type recursive mergesort for comparable items (#15406)
Accept any iterable of mutually comparable items and return a new list.
Part of #15234
2026-09-23 08:10:19 +02:00
Nikhil NagpureandChristian Clauss 763b146ef2 feat(sorts): make reversort generic over comparable items (#15402)
* feat(sorts): make reversort generic over comparable items

Part of #15234

- Replace list[Any] with a Comparable-bounded TypeVar so reversort and
  reversort_cost sort any mutually comparable items, not just ints
- Add TypeError doctests confirming mixed non-comparable input raises
- Register reversort in the shared test battery covering str/float/
  dataclass/NamedTuple cases and the non-comparable rejection path

* style(sorts): use PEP 695 type parameters per repo convention

ruff UP047 requires the sanctioned [T: Comparable] signature form used
by the reference insertion_sort.py. Keep the module-level TypeVar as in
the reference.

* Apply batched suggestions from code review

Co-authored-by: Christian Clauss <cclauss@me.com>

---------

Co-authored-by: Christian Clauss <cclauss@me.com>
2026-09-22 23:21:26 +02:00
peeyushtiwary27pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>Christian Clauss
e278aed789 sorts: make bogo_sort generic for comparable items (#15401)
* sorts: make bogo_sort generic for comparable items

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update tests/test_sorts.py

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
2026-09-22 19:06:45 +02:00
nikhil-mewarnikhil.mewar@valiancesolutions.com <nikhil.mewar@valiancesolutions.com>Christian Clauss
612f5188e3 sorts: make strand sort work with comparable types (#15350)
* sorts: make strand sort work with comparable types

* Add type parameter to strand_sort function

* Add strand_sort to sorting tests

---------

Co-authored-by: nikhil.mewar@valiancesolutions.com <nikhil.mewar@valiancesolutions.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
2026-09-21 18:50:09 +02:00
RokeshwaranandChristian Clauss 9ec2f097fe sorts: make recursive_insertion_sort generic over Comparable items (#15340)
* sorts: make recursive_insertion_sort generic over Comparable items

Part of #15234

- switch rec_insertion_sort/insert_next to the Comparable/TypeVar-bound
  MutableSequence[T] pattern (in-place sorts bucket) per the convention
  discussed on #15234
- make rec_insertion_sort return the sorted collection and give n a
  default of len(collection), so it can be called with a single
  argument like the other sorts in the shared test battery
- add a string doctest
- register rec_insertion_sort in tests/test_sorts.py's shared SORTS
  battery and the non-comparable-items rejection test

* fix: keep rec_insertion_sort in-place with None return, per maintainer review

- Revert n to a required parameter and drop the MutableSequence[T] return
  value; rec_insertion_sort stays a pure in-place sort returning None, as
  requested in review.
- Keep the Comparable/TypeVar generalization (no PEP 695 syntax, to match
  the existing TypeVar style in the file).
- Keep the non-int (string) doctest.
- tests/test_sorts.py: rec_insertion_sort no longer fits the shared SORTS
  battery (which asserts on a returned value), so it's removed from that
  tuple and given its own parametrized in-place test, plus its own
  non-comparable-items rejection test.

* test: adjust test_sorts.py for rec_insertion_sort's None-returning in-place contract

rec_insertion_sort no longer fits the shared SORTS battery (which asserts
on a returned value), so it's removed from that tuple and given its own
parametrized in-place test (checked against sorted()) plus its own
non-comparable-items rejection test.

* Apply batched suggestions from code review

Co-authored-by: Christian Clauss <cclauss@me.com>

---------

Co-authored-by: Christian Clauss <cclauss@me.com>
2026-09-21 14:03:36 +02:00
Aayush Guptapre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>Christian Clauss
219478db6e sorts: make odd_even_sort generic over any comparable type (Part of #15234) (#15378)
* sorts: make odd_even_sort generic over any comparable type (Part of #15234)

Adds a Comparable-bound TypeVar (matching the pattern used in insertion_sort.py), doctests covering strings, floats, and the non-comparable TypeError case, and registers odd_even_sort in the shared test_sort_rejects_non_comparable_items test.

Part of #15234

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Drop redundant module-level TypeVar, bind Comparable to __gt__

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Fix import block formatting per ruff

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
2026-09-21 14:01:45 +02:00
RokeshwaranandChristian Clauss 56858d6126 sorts: make pancake_sort generic over Comparable items (#15311)
Part of #15234

- switch pancake_sort's TypeVar to the Comparable/TypeVar-bound
  pattern used by insertion_sort.py
- add doctests covering strings, floats, and random data
- register pancake_sort in tests/test_sorts.py's shared battery and
  the non-comparable-items rejection test

Co-authored-by: Christian Clauss <cclauss@me.com>
2026-09-18 03:05:44 +02:00
Rayan-and-beyondandRayan-and-beyond 092b9d0be8 sorts: make cycle_sort generic for comparable items (#15329)
Co-authored-by: Rayan-and-beyond <263488867+Rayan-and-beyond@users.noreply.github.com>
2026-09-18 02:46:54 +02:00
Talandtaljeon 60cf84cedc sorts: type shrink shell sort for comparable items (#15334)
Co-authored-by: taljeon <taljeon@users.noreply.github.com>
2026-09-18 02:46:24 +02:00
余枫andChristian Clauss 4dcd68eedd sorts: make exchange_sort generic for comparable items (#15304)
* sorts: make exchange_sort generic for comparable items

* Ints and floats are comparable

---------

Co-authored-by: Christian Clauss <cclauss@me.com>
2026-09-12 22:34:45 +02:00
Christian Clauss 0525ef5da8 ruff rule ANN202 missing-return-type-private-function (#15298)
* ruff rule ANN202 missing-return-type-private-function

* ruff rule ANN202 missing-return-type-private-function
2026-09-12 21:47:08 +02:00
Orji PatriciaandChristian Clauss d502013438 sorts: make comb_sort generic for comparable items (#15288)
* sorts: make comb sort generic for comparable items

* tests: cover comb sort incomparable inputs

* Update sorts/comb_sort.py

* Apply suggestion from @cclauss

* Apply suggestion from @cclauss

---------

Co-authored-by: Christian Clauss <cclauss@me.com>
2026-09-12 07:53:55 +02:00
Ge YingshanandGavin-Yau 26e4320885 sorts: type cocktail shaker sort for comparable items (#15278)
Co-authored-by: Gavin-Yau <2695188238@qq.com>
2026-09-11 23:59:59 +02:00
bpiper02 6a8f4ed4bf Make gnome sort generic over comparable items (#15261) 2026-09-10 17:50:21 +02:00
Christian Clauss 8a138809fa test_sorts with dataclasses and namedtuples (#15260) 2026-09-10 05:19:12 +02:00
Hanazar OchikawaandChristian Clauss 9e1cf59796 sorts: support comparable items in circle_sort (#15244)
* sorts: support comparable items in circle_sort

* style: format circle_sort

* Apply batched suggestions from code review

Co-authored-by: Christian Clauss <cclauss@me.com>

---------

Co-authored-by: Christian Clauss <cclauss@me.com>
2026-09-09 22:31:35 +00:00
Harsh Raj SinghaniaHarsh Raj SinghaniaChristian Clausspre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
5ce67332fc sorts: type bubble sort for any comparable items (#15240)
* sorts: type bubble sort for any comparable items

Bound bubble_sort_iterative and bubble_sort_recursive to a Comparable protocol instead of Any, add TypeError doctests, and cover the mixed-type failure in tests/test_sorts.py.

Refs #15234

* Update tests/test_sorts.py

* Add binary insertion sort to tests and rename function

Updated tests to include binary insertion sort and renamed test function.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: Harsh Raj Singhania <harshrajsinghania@proton.me>
Co-authored-by: Christian Clauss <cclauss@me.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2026-09-09 16:55:55 +02:00
Sneha Kumaripre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>Christian Clauss
d0f9b6e061 Improve binary insertion sort comparable typing (#15238)
* Improve binary insertion sort comparable typing

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update binary_insertion_sort function signature

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
2026-09-09 15:41:01 +02:00
priya-sundaram-dev 9cac6fb26c tests: parametrize sorts/ over a shared input battery (#15185) 2026-09-04 20:57:54 +02:00
NavanithaSripre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>Christian Clauss
818ecfa4cb Add pytest for heap_sort (#13231)
* Add pytest for heap_sort

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Add __init__.py to tests to fix Ruff namespace package error

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
2026-09-04 19:52:14 +02:00