From 971331b5ef850704242c21065cb0c41cf88b73d3 Mon Sep 17 00:00:00 2001 From: Ramvelivela Date: Thu, 24 Sep 2026 12:24:56 +0530 Subject: [PATCH 1/3] Make bitonic sort support comparable items --- sorts/bitonic_sort.py | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/sorts/bitonic_sort.py b/sorts/bitonic_sort.py index 600f8139603a..faafac12d28e 100644 --- a/sorts/bitonic_sort.py +++ b/sorts/bitonic_sort.py @@ -6,8 +6,14 @@ from __future__ import annotations +from typing import Protocol -def comp_and_swap(array: list[int], index1: int, index2: int, direction: int) -> None: + +class Comparable(Protocol): + def __lt__(self, other: object, /) -> bool: ... + + +def comp_and_swap[T: Coparable](array: list[T], index1: int, index2: int, direction: int) -> None: """Compare the value at given index1 and index2 of the array and swap them as per the given direction. @@ -38,7 +44,7 @@ def comp_and_swap(array: list[int], index1: int, index2: int, direction: int) -> array[index1], array[index2] = array[index2], array[index1] -def bitonic_merge(array: list[int], low: int, length: int, direction: int) -> None: +def bitonic_merge[T: Comparable](array: list[T], low: int, length: int, direction: int) -> None: """ It recursively sorts a bitonic sequence in ascending order, if direction = 1, and in descending if direction = 0. @@ -62,7 +68,7 @@ def bitonic_merge(array: list[int], low: int, length: int, direction: int) -> No bitonic_merge(array, low + middle, middle, direction) -def bitonic_sort(array: list[int], low: int, length: int, direction: int) -> None: +def bitonic_sort[T: Comparable](array: list[T], low: int, length: int, direction: int) -> None: """ This function first produces a bitonic sequence by recursively sorting its two halves in opposite sorting orders, and then calls bitonic_merge to make them in the @@ -76,6 +82,22 @@ def bitonic_sort(array: list[int], low: int, length: int, direction: int) -> Non >>> bitonic_sort(arr, 0, 8, 0) >>> arr [145, 92, 34, 12, 0, -23, -121, -167] + + >>> arr = ["banana", "apple", "cherry","date"] + >>> bitonic_sort(arr, 0, 4, 1) + >>> arr + ['apple', 'banana', 'cherry', 'date'] + + >>> arr = [3, 1.5, 2, 4.5] + >>> bitonic_sort(arr, 0, 4, 1) + >>> arr + [1.5, 2, 3, 4.5] + + >>> arr = [1, "two", 3, "four"] + >>> bitonic_sort(arr, 0, 4, 1) + Traceback (most recent call last): + ... + TypeError: '>' not supported between instances of 'int' and 'str' """ if length > 1: middle = int(length / 2) From 3d78035d4e01e83fd81347ef94fdbcb5ae841163 Mon Sep 17 00:00:00 2001 From: Ramvelivela Date: Thu, 24 Sep 2026 14:20:10 +0530 Subject: [PATCH 2/3] Add comparable item tests for bitonic sort --- sorts/bitonic_sort.py | 18 ++++++++++++------ tests/test_sorts.py | 14 ++++++++++++++ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/sorts/bitonic_sort.py b/sorts/bitonic_sort.py index faafac12d28e..f2cf04d8c681 100644 --- a/sorts/bitonic_sort.py +++ b/sorts/bitonic_sort.py @@ -13,7 +13,9 @@ class Comparable(Protocol): def __lt__(self, other: object, /) -> bool: ... -def comp_and_swap[T: Coparable](array: list[T], index1: int, index2: int, direction: int) -> None: +def comp_and_swap[T: Comparable]( + array: list[T], index1: int, index2: int, direction: int +) -> None: """Compare the value at given index1 and index2 of the array and swap them as per the given direction. @@ -38,13 +40,15 @@ def comp_and_swap[T: Coparable](array: list[T], index1: int, index2: int, direct >>> arr [12, 42, -21, 1] """ - if (direction == 1 and array[index1] > array[index2]) or ( + if (direction == 1 and array[index2] < array[index1]) or ( direction == 0 and array[index1] < array[index2] ): array[index1], array[index2] = array[index2], array[index1] -def bitonic_merge[T: Comparable](array: list[T], low: int, length: int, direction: int) -> None: +def bitonic_merge[T: Comparable]( + array: list[T], low: int, length: int, direction: int +) -> None: """ It recursively sorts a bitonic sequence in ascending order, if direction = 1, and in descending if direction = 0. @@ -68,7 +72,9 @@ def bitonic_merge[T: Comparable](array: list[T], low: int, length: int, directio bitonic_merge(array, low + middle, middle, direction) -def bitonic_sort[T: Comparable](array: list[T], low: int, length: int, direction: int) -> None: +def bitonic_sort[T: Comparable]( + array: list[T], low: int, length: int, direction: int +) -> None: """ This function first produces a bitonic sequence by recursively sorting its two halves in opposite sorting orders, and then calls bitonic_merge to make them in the @@ -83,7 +89,7 @@ def bitonic_sort[T: Comparable](array: list[T], low: int, length: int, direction >>> arr [145, 92, 34, 12, 0, -23, -121, -167] - >>> arr = ["banana", "apple", "cherry","date"] + >>> arr = ["banana", "apple", "cherry", "date"] >>> bitonic_sort(arr, 0, 4, 1) >>> arr ['apple', 'banana', 'cherry', 'date'] @@ -97,7 +103,7 @@ def bitonic_sort[T: Comparable](array: list[T], low: int, length: int, direction >>> bitonic_sort(arr, 0, 4, 1) Traceback (most recent call last): ... - TypeError: '>' not supported between instances of 'int' and 'str' + TypeError: '<' not supported between instances of 'str' and 'int' """ if length > 1: middle = int(length / 2) diff --git a/tests/test_sorts.py b/tests/test_sorts.py index d18be1c22b83..5f515c03bb8b 100644 --- a/tests/test_sorts.py +++ b/tests/test_sorts.py @@ -172,3 +172,17 @@ def test_bogo_sort_comparable_items() -> None: with pytest.raises(TypeError): bogo_sort([1, "a"]) + +def test_bitonic_sort_comparable_items() -> None: + from sorts.bitonic_sort import bitonic_sort + + strings = ["banana", "apple", "cherry", "date"] + bitonic_sort(strings, 0, len(strings), 1) + assert strings == ["apple", "banana", "cherry", "date"] + + numbers = [3, 1.5, 2, 4.5] + bitonic_sort(numbers, 0, len(numbers), 1) + assert numbers == [1.5, 2, 3, 4.5] + + with pytest.raises(TypeError): + bitonic_sort([1, "two", 3, "four"], 0, 4, 1) \ No newline at end of file From 568ac1a82766b642777e27e379f4b25a1cb2fc57 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 24 Sep 2026 09:26:08 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_sorts.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_sorts.py b/tests/test_sorts.py index 5f515c03bb8b..6cd350b86e2e 100644 --- a/tests/test_sorts.py +++ b/tests/test_sorts.py @@ -173,6 +173,7 @@ def test_bogo_sort_comparable_items() -> None: with pytest.raises(TypeError): bogo_sort([1, "a"]) + def test_bitonic_sort_comparable_items() -> None: from sorts.bitonic_sort import bitonic_sort @@ -185,4 +186,4 @@ def test_bitonic_sort_comparable_items() -> None: assert numbers == [1.5, 2, 3, 4.5] with pytest.raises(TypeError): - bitonic_sort([1, "two", 3, "four"], 0, 4, 1) \ No newline at end of file + bitonic_sort([1, "two", 3, "four"], 0, 4, 1)