@@ -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 )
0 commit comments