Skip to content

Commit 3d78035

Browse files
committed
Add comparable item tests for bitonic sort
1 parent 971331b commit 3d78035

2 files changed

Lines changed: 26 additions & 6 deletions

File tree

‎sorts/bitonic_sort.py‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ class Comparable(Protocol):
1313
def __lt__(self, other: object, /) -> bool: ...
1414

1515

16-
def comp_and_swap[T: Coparable](array: list[T], index1: int, index2: int, direction: int) -> None:
16+
def comp_and_swap[T: Comparable](
17+
array: list[T], index1: int, index2: int, direction: int
18+
) -> None:
1719
"""Compare the value at given index1 and index2 of the array and swap them as per
1820
the given direction.
1921
@@ -38,13 +40,15 @@ def comp_and_swap[T: Coparable](array: list[T], index1: int, index2: int, direct
3840
>>> arr
3941
[12, 42, -21, 1]
4042
"""
41-
if (direction == 1 and array[index1] > array[index2]) or (
43+
if (direction == 1 and array[index2] < array[index1]) or (
4244
direction == 0 and array[index1] < array[index2]
4345
):
4446
array[index1], array[index2] = array[index2], array[index1]
4547

4648

47-
def bitonic_merge[T: Comparable](array: list[T], low: int, length: int, direction: int) -> None:
49+
def bitonic_merge[T: Comparable](
50+
array: list[T], low: int, length: int, direction: int
51+
) -> None:
4852
"""
4953
It recursively sorts a bitonic sequence in ascending order, if direction = 1, and in
5054
descending if direction = 0.
@@ -68,7 +72,9 @@ def bitonic_merge[T: Comparable](array: list[T], low: int, length: int, directio
6872
bitonic_merge(array, low + middle, middle, direction)
6973

7074

71-
def bitonic_sort[T: Comparable](array: list[T], low: int, length: int, direction: int) -> None:
75+
def bitonic_sort[T: Comparable](
76+
array: list[T], low: int, length: int, direction: int
77+
) -> None:
7278
"""
7379
This function first produces a bitonic sequence by recursively sorting its two
7480
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
8389
>>> arr
8490
[145, 92, 34, 12, 0, -23, -121, -167]
8591
86-
>>> arr = ["banana", "apple", "cherry","date"]
92+
>>> arr = ["banana", "apple", "cherry", "date"]
8793
>>> bitonic_sort(arr, 0, 4, 1)
8894
>>> arr
8995
['apple', 'banana', 'cherry', 'date']
@@ -97,7 +103,7 @@ def bitonic_sort[T: Comparable](array: list[T], low: int, length: int, direction
97103
>>> bitonic_sort(arr, 0, 4, 1)
98104
Traceback (most recent call last):
99105
...
100-
TypeError: '>' not supported between instances of 'int' and 'str'
106+
TypeError: '<' not supported between instances of 'str' and 'int'
101107
"""
102108
if length > 1:
103109
middle = int(length / 2)

‎tests/test_sorts.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,17 @@ def test_bogo_sort_comparable_items() -> None:
172172

173173
with pytest.raises(TypeError):
174174
bogo_sort([1, "a"])
175+
176+
def test_bitonic_sort_comparable_items() -> None:
177+
from sorts.bitonic_sort import bitonic_sort
178+
179+
strings = ["banana", "apple", "cherry", "date"]
180+
bitonic_sort(strings, 0, len(strings), 1)
181+
assert strings == ["apple", "banana", "cherry", "date"]
182+
183+
numbers = [3, 1.5, 2, 4.5]
184+
bitonic_sort(numbers, 0, len(numbers), 1)
185+
assert numbers == [1.5, 2, 3, 4.5]
186+
187+
with pytest.raises(TypeError):
188+
bitonic_sort([1, "two", 3, "four"], 0, 4, 1)

0 commit comments

Comments
 (0)