12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
- from ._utils import core , generate , convert
15
+ from ._utils import generate , convert
16
16
from ._typing import Iterable , Callable , Optional , _T , SupportsRichComparison , List
17
17
18
18
def bubble (__iterable : Iterable [_T ], key : Optional [Callable [[_T ], SupportsRichComparison ]]= None , reverse : bool = False ) -> List [_T ]:
@@ -26,7 +26,7 @@ def bubble(__iterable: Iterable[_T], key: Optional[Callable[[_T], SupportsRichCo
26
26
for i in range (len (__iterable ) - 1 ):
27
27
flag : bool = False # early stop by adding a bool value named flag
28
28
for j in range (len (__iterable ) - i - 1 ):
29
- if core (compare [j ], compare [j + 1 ], key , reverse ):
29
+ if (compare [j ] < compare [j + 1 ] if reverse else compare [ j ] > compare [ j + 1 ] ):
30
30
__iterable [j ], __iterable [j + 1 ] = __iterable [j + 1 ], __iterable [j ]
31
31
flag : bool = True
32
32
if key != None :
@@ -50,7 +50,7 @@ def insert(__iterable: Iterable[_T], key: Optional[Callable[[_T], SupportsRichCo
50
50
high : int = index - 1
51
51
while low <= high : # sequence conforming to monotonicity
52
52
mid : int = (low + high ) // 2
53
- if core (keyc , compare [mid ], key , reverse ):
53
+ if (keyc < compare [mid ] if reverse else keyc > compare [ mid ] ):
54
54
low : int = mid + 1
55
55
else :
56
56
high : int = mid - 1
@@ -78,7 +78,7 @@ def shell(__iterable: Iterable[_T], key: Optional[Callable[[_T], SupportsRichCom
78
78
while gap >= 1 :
79
79
for index in range (gap , length ):
80
80
next : int = index
81
- while next >= gap and core (compare [next - gap ], compare [next ], key , reverse ):
81
+ while next >= gap and (compare [next - gap ] < compare [next ] if reverse else compare [ next - gap ] > compare [ next ] ):
82
82
__iterable [next ], __iterable [next - gap ] = __iterable [next - gap ], __iterable [next ]
83
83
if key != None :
84
84
compare [next ], compare [next - gap ] = compare [next - gap ], compare [next ]
@@ -102,9 +102,9 @@ def build(root: int, end: int) -> None:
102
102
piv : int = root
103
103
left : int = 2 * root + 1
104
104
right : int = 2 * root + 2
105
- if left < end and core (compare [left ], compare [root ], key , reverse ):
105
+ if left < end and (compare [left ] < compare [root ] if reverse else compare [ left ] > compare [ root ] ):
106
106
piv : int = left
107
- if right < end and core (compare [right ], compare [piv ], key , reverse ):
107
+ if right < end and (compare [right ] < compare [piv ] if reverse else compare [ right ] > compare [ piv ] ):
108
108
piv : int = right
109
109
if piv != root :
110
110
__iterable [root ], __iterable [piv ] = __iterable [piv ], __iterable [root ]
@@ -147,7 +147,7 @@ def partition(l: int, r: int) -> int:
147
147
val : _T = compare [r ]
148
148
index : int = l - 1
149
149
for ind in range (l , r ):
150
- if core (val , compare [ind ], key , reverse ):
150
+ if (val < compare [ind ] if reverse else val > compare [ ind ] ):
151
151
index += 1
152
152
__iterable [index ], __iterable [ind ] = __iterable [ind ], __iterable [index ]
153
153
if key != None :
@@ -182,7 +182,7 @@ def merg(low: int, mid: int, high: int) -> None:
182
182
result : List [_T ] = []
183
183
store : List [_T ] = []
184
184
while i < len (left ) and j < len (right ):
185
- if core (rc [j ], lc [i ], key , reverse ):
185
+ if (rc [j ] < lc [i ] if reverse else rc [ j ] > lc [ i ] ):
186
186
result .append (left [i ])
187
187
store .append (lc [i ])
188
188
i += 1
0 commit comments