12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
- from ._utils import generate , convert
15
+ from ._utils import generate , convert , verify
16
16
from ._typing import Iterable , Callable , Optional , _T , SupportsRichComparison , List
17
17
18
18
__all__ = ["bubble" , "insert" , "shell" , "heap" , "quick" , "merge" ]
@@ -27,16 +27,17 @@ def bubble(__iterable: Iterable[_T], key: Optional[Callable[[_T], SupportsRichCo
27
27
'''
28
28
__iterable : List [_T ] = convert (__iterable )
29
29
compare : List [_T ] = generate (__iterable , key )
30
- for i in range (len (__iterable ) - 1 ):
31
- flag : bool = False # early stop by adding a bool value named flag
32
- for j in range (len (__iterable ) - i - 1 ):
33
- if (compare [j ] < compare [j + 1 ] if reverse else compare [j ] > compare [j + 1 ]):
34
- __iterable [j ], __iterable [j + 1 ] = __iterable [j + 1 ], __iterable [j ]
35
- if key != None :
36
- compare [j ], compare [j + 1 ] = compare [j + 1 ], compare [j ]
37
- flag : bool = True
38
- if not flag :
39
- break
30
+ if compare and not verify (compare ):
31
+ for i in range (len (__iterable ) - 1 ):
32
+ flag : bool = False # early stop by adding a bool value named flag
33
+ for j in range (len (__iterable ) - i - 1 ):
34
+ if (compare [j ] < compare [j + 1 ] if reverse else compare [j ] > compare [j + 1 ]):
35
+ __iterable [j ], __iterable [j + 1 ] = __iterable [j + 1 ], __iterable [j ]
36
+ if key != None :
37
+ compare [j ], compare [j + 1 ] = compare [j + 1 ], compare [j ]
38
+ flag : bool = True
39
+ if not flag :
40
+ break
40
41
return __iterable
41
42
42
43
def insert (__iterable : Iterable [_T ], key : Optional [Callable [[_T ], SupportsRichComparison ]]= None , reverse : bool = False ) -> List [_T ]:
@@ -49,24 +50,25 @@ def insert(__iterable: Iterable[_T], key: Optional[Callable[[_T], SupportsRichCo
49
50
'''
50
51
__iterable : List [_T ] = convert (__iterable )
51
52
compare : List [_T ] = generate (__iterable , key )
52
- for index in range (1 , len (__iterable )):
53
- keyc : _T = compare [index ]
54
- keya : _T = __iterable [index ]
55
- low : int = 0
56
- high : int = index - 1
57
- while low <= high : # sequence conforming to monotonicity
58
- mid : int = (low + high ) // 2
59
- if (keyc < compare [mid ] if reverse else keyc > compare [mid ]):
60
- low : int = mid + 1
61
- else :
62
- high : int = mid - 1
63
- for pre in range (index , low , - 1 ): # from back to front
64
- __iterable [pre ] = __iterable [pre - 1 ]
53
+ if compare and not verify (compare ):
54
+ for index in range (1 , len (__iterable )):
55
+ keyc : _T = compare [index ]
56
+ keya : _T = __iterable [index ]
57
+ low : int = 0
58
+ high : int = index - 1
59
+ while low <= high : # sequence conforming to monotonicity
60
+ mid : int = (low + high ) // 2
61
+ if (keyc < compare [mid ] if reverse else keyc > compare [mid ]):
62
+ low : int = mid + 1
63
+ else :
64
+ high : int = mid - 1
65
+ for pre in range (index , low , - 1 ): # from back to front
66
+ __iterable [pre ] = __iterable [pre - 1 ]
67
+ if key != None :
68
+ compare [pre ] = compare [pre - 1 ]
69
+ __iterable [low ] = keya
65
70
if key != None :
66
- compare [pre ] = compare [pre - 1 ]
67
- __iterable [low ] = keya
68
- if key != None :
69
- compare [low ] = keyc
71
+ compare [low ] = keyc
70
72
return __iterable
71
73
72
74
def shell (__iterable : Iterable [_T ], key : Optional [Callable [[_T ], SupportsRichComparison ]]= None , reverse : bool = False ) -> List [_T ]:
@@ -79,19 +81,20 @@ def shell(__iterable: Iterable[_T], key: Optional[Callable[[_T], SupportsRichCom
79
81
'''
80
82
__iterable : List [_T ] = convert (__iterable )
81
83
compare : List [_T ] = generate (__iterable , key )
82
- length : int = len (__iterable )
83
- gap : int = 1
84
- while gap < length / 3 :
85
- gap : int = int (3 * gap + 1 )
86
- while gap >= 1 :
87
- for index in range (gap , length ):
88
- next : int = index
89
- while next >= gap and (compare [next - gap ] < compare [next ] if reverse else compare [next - gap ] > compare [next ]):
90
- __iterable [next ], __iterable [next - gap ] = __iterable [next - gap ], __iterable [next ]
91
- if key != None :
92
- compare [next ], compare [next - gap ] = compare [next - gap ], compare [next ]
93
- next -= gap
94
- gap : int = int (gap / 3 )
84
+ if compare and not verify (compare ):
85
+ length : int = len (__iterable )
86
+ gap : int = 1
87
+ while gap < length / 3 :
88
+ gap : int = int (3 * gap + 1 )
89
+ while gap >= 1 :
90
+ for index in range (gap , length ):
91
+ next : int = index
92
+ while next >= gap and (compare [next - gap ] < compare [next ] if reverse else compare [next - gap ] > compare [next ]):
93
+ __iterable [next ], __iterable [next - gap ] = __iterable [next - gap ], __iterable [next ]
94
+ if key != None :
95
+ compare [next ], compare [next - gap ] = compare [next - gap ], compare [next ]
96
+ next -= gap
97
+ gap : int = int (gap / 3 )
95
98
return __iterable
96
99
97
100
def heap (__iterable : Iterable [_T ], key : Optional [Callable [[_T ], SupportsRichComparison ]]= None , reverse : bool = False ) -> List [_T ]:
@@ -121,15 +124,15 @@ def build(root: int, end: int) -> None:
121
124
if key != None :
122
125
compare [root ], compare [piv ] = compare [piv ], compare [root ]
123
126
build (piv , end )
124
-
125
- length : int = len (__iterable )
126
- for root in range (length // 2 - 1 , - 1 , - 1 ):
127
- build (root , length )
128
- for end in range (length - 1 , 0 , - 1 ):
129
- __iterable [0 ], __iterable [end ] = __iterable [end ], __iterable [0 ]
130
- if key != None :
131
- compare [0 ], compare [end ] = compare [end ], compare [0 ]
132
- build (0 , end )
127
+ if compare and not verify ( compare ):
128
+ length : int = len (__iterable )
129
+ for root in range (length // 2 - 1 , - 1 , - 1 ):
130
+ build (root , length )
131
+ for end in range (length - 1 , 0 , - 1 ):
132
+ __iterable [0 ], __iterable [end ] = __iterable [end ], __iterable [0 ]
133
+ if key != None :
134
+ compare [0 ], compare [end ] = compare [end ], compare [0 ]
135
+ build (0 , end )
133
136
return __iterable
134
137
135
138
def quick (__iterable : Iterable [_T ], key : Optional [Callable [[_T ], SupportsRichComparison ]]= None , reverse : bool = False ) -> List [_T ]:
@@ -168,7 +171,8 @@ def partition(l: int, r: int) -> int:
168
171
if key != None :
169
172
compare [index + 1 ], compare [r ] = compare [r ], compare [index + 1 ]
170
173
return index + 1
171
- solve (0 , len (__iterable ) - 1 )
174
+ if compare and not verify (compare ):
175
+ solve (0 , len (__iterable ) - 1 )
172
176
return __iterable
173
177
174
178
def merge (__iterable : Iterable [_T ], key : Optional [Callable [[_T ], SupportsRichComparison ]]= None , reverse : bool = False ) -> List [_T ]:
@@ -225,5 +229,6 @@ def solve() -> None:
225
229
merg (low , mid , high )
226
230
low += 2 * i
227
231
i *= 2
228
- solve ()
232
+ if compare and not verify (compare ):
233
+ solve ()
229
234
return __iterable
0 commit comments