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
15
+ from ._utils import core , generate , convert
16
16
from ._typing import Iterable , Callable , Optional , _T , SupportsRichComparison
17
17
18
- def bubble (array : Iterable [_T ], key : Optional [Callable [[_T ], SupportsRichComparison ]]= None , reverse : bool = False ) -> None :
18
+ def bubble (__iterable : Iterable [_T ], key : Optional [Callable [[_T ], SupportsRichComparison ]]= None , reverse : bool = False ) -> list :
19
19
'''
20
- :param array : iterable data.
20
+ :param __iterable : iterable data.
21
21
:param key: callable function, for example: key=lambda x: x[1], key=lambda x: (x[0], x[1]).
22
22
:param reverse: whether to use descending order. The default is ascending order.
23
23
'''
24
- compare = generate (array , key )
25
- for i in range (len (array ) - 1 ):
24
+ __iterable = convert (__iterable )
25
+ compare = generate (__iterable , key )
26
+ for i in range (len (__iterable ) - 1 ):
26
27
flag = False # early stop by adding a bool value named flag
27
- for j in range (len (array ) - i - 1 ):
28
+ for j in range (len (__iterable ) - i - 1 ):
28
29
if core (compare [j ], compare [j + 1 ], key , reverse ):
29
- array [j ], array [j + 1 ] = array [j + 1 ], array [j ]
30
+ __iterable [j ], __iterable [j + 1 ] = __iterable [j + 1 ], __iterable [j ]
30
31
flag = True
31
32
if key != None :
32
33
compare [j ], compare [j + 1 ] = compare [j + 1 ], compare [j ]
33
34
if not flag :
34
35
break
36
+ return __iterable
35
37
36
- def insert (array : Iterable [_T ], key : Optional [Callable [[_T ], SupportsRichComparison ]]= None , reverse : bool = False ) -> None :
38
+ def insert (__iterable : Iterable [_T ], key : Optional [Callable [[_T ], SupportsRichComparison ]]= None , reverse : bool = False ) -> list :
37
39
'''
38
- :param array : iterable data.
40
+ :param __iterable : iterable data.
39
41
:param key: callable function, for example: key=lambda x: x[1], key=lambda x: (x[0], x[1]).
40
42
:param reverse: whether to use descending order. The default is ascending order.
41
43
'''
42
- compare = generate (array , key )
43
- for index in range (1 , len (array )):
44
- keyc , keya = compare [index ], array [index ]
44
+ __iterable = convert (__iterable )
45
+ compare = generate (__iterable , key )
46
+ for index in range (1 , len (__iterable )):
47
+ keyc , keya = compare [index ], __iterable [index ]
45
48
low , high = 0 , index - 1
46
49
while low <= high : # sequence conforming to monotonicity
47
50
mid = (low + high ) // 2
@@ -50,45 +53,50 @@ def insert(array: Iterable[_T], key: Optional[Callable[[_T], SupportsRichCompari
50
53
else :
51
54
high = mid - 1
52
55
for pre in range (index , low , - 1 ): # from back to front
53
- array [pre ] = array [pre - 1 ]
56
+ __iterable [pre ] = __iterable [pre - 1 ]
54
57
if key != None :
55
58
compare [pre ] = compare [pre - 1 ]
56
- array [low ] = keya
59
+ __iterable [low ] = keya
57
60
if key != None :
58
61
compare [low ] = keyc
62
+ return __iterable
59
63
60
- def shell (array : Iterable [_T ], key : Optional [Callable [[_T ], SupportsRichComparison ]]= None , reverse : bool = False ) -> None :
64
+ def shell (__iterable : Iterable [_T ], key : Optional [Callable [[_T ], SupportsRichComparison ]]= None , reverse : bool = False ) -> list :
61
65
'''
62
- :param array : iterable data.
66
+ :param __iterable : iterable data.
63
67
:param key: callable function, for example: key=lambda x: x[1], key=lambda x: (x[0], x[1]).
64
68
:param reverse: whether to use descending order. The default is ascending order.
65
69
'''
66
- compare = generate (array , key )
67
- length = len (array )
70
+ __iterable = convert (__iterable )
71
+ compare = generate (__iterable , key )
72
+ length = len (__iterable )
68
73
gap = 1
69
74
while gap < length / 3 :
70
75
gap = int (3 * gap + 1 )
71
76
while gap >= 1 :
72
77
for index in range (gap , length ):
73
78
next = index
74
79
while next >= gap and core (compare [next - gap ], compare [next ], key , reverse ):
75
- array [next ], array [next - gap ] = array [next - gap ], array [next ]
80
+ __iterable [next ], __iterable [next - gap ] = __iterable [next - gap ], __iterable [next ]
76
81
if key != None :
77
82
compare [next ], compare [next - gap ] = compare [next - gap ], compare [next ]
78
83
next -= gap
79
84
gap = int (gap / 3 )
85
+ return __iterable
86
+
80
87
81
- def heap (array : Iterable [_T ], key : Optional [Callable [[_T ], SupportsRichComparison ]]= None , reverse : bool = False ) -> None :
88
+ def heap (__iterable : Iterable [_T ], key : Optional [Callable [[_T ], SupportsRichComparison ]]= None , reverse : bool = False ) -> list :
82
89
'''
83
- :param array : iterable data.
90
+ :param __iterable : iterable data.
84
91
:param key: callable function, for example: key=lambda x: x[1], key=lambda x: (x[0], x[1]).
85
92
:param reverse: whether to use descending order. The default is ascending order.
86
93
'''
87
- compare = generate (array , key )
94
+ __iterable = convert (__iterable )
95
+ compare = generate (__iterable , key )
88
96
def build (root : int , end : int ) -> None :
89
97
'''
90
98
:param root: cursor indicating the root node (int).
91
- :param end: cursor indicating the end of the array (int).
99
+ :param end: cursor indicating the end of the __iterable (int).
92
100
'''
93
101
piv = root
94
102
left = 2 * root + 1
@@ -98,27 +106,29 @@ def build(root: int, end: int) -> None:
98
106
if right < end and core (compare [right ], compare [piv ], key , reverse ):
99
107
piv = right
100
108
if piv != root :
101
- array [root ], array [piv ] = array [piv ], array [root ]
109
+ __iterable [root ], __iterable [piv ] = __iterable [piv ], __iterable [root ]
102
110
if key != None :
103
111
compare [root ], compare [piv ] = compare [piv ], compare [root ]
104
112
build (piv , end )
105
113
106
- length = len (array )
114
+ length = len (__iterable )
107
115
for root in range (length // 2 - 1 , - 1 , - 1 ):
108
116
build (root , length )
109
117
for end in range (length - 1 , 0 , - 1 ):
110
- array [0 ], array [end ] = array [end ], array [0 ]
118
+ __iterable [0 ], __iterable [end ] = __iterable [end ], __iterable [0 ]
111
119
if key != None :
112
120
compare [0 ], compare [end ] = compare [end ], compare [0 ]
113
121
build (0 , end )
122
+ return __iterable
114
123
115
- def quick (array : Iterable [_T ], key : Optional [Callable [[_T ], SupportsRichComparison ]]= None , reverse : bool = False ) -> None :
124
+ def quick (__iterable : Iterable [_T ], key : Optional [Callable [[_T ], SupportsRichComparison ]]= None , reverse : bool = False ) -> list :
116
125
'''
117
- :param array : iterable data.
126
+ :param __iterable : iterable data.
118
127
:param key: callable function, for example: key=lambda x: x[1], key=lambda x: (x[0], x[1]).
119
128
:param reverse: whether to use descending order. The default is ascending order.
120
129
'''
121
- compare = generate (array , key )
130
+ __iterable = convert (__iterable )
131
+ compare = generate (__iterable , key )
122
132
def solve (l : int , r : int ) -> None :
123
133
'''
124
134
main
@@ -130,38 +140,40 @@ def solve(l: int, r: int) -> None:
130
140
131
141
def partition (l : int , r : int ) -> int :
132
142
'''
133
- :param l: The left cursor of array (int).
134
- :param r: The right cursor of array (int).
143
+ :param l: The left cursor of __iterable (int).
144
+ :param r: The right cursor of __iterable (int).
135
145
'''
136
146
val = compare [r ]
137
147
index = l - 1
138
148
for ind in range (l , r ):
139
149
if core (val , compare [ind ], key , reverse ):
140
150
index += 1
141
- array [index ], array [ind ] = array [ind ], array [index ]
151
+ __iterable [index ], __iterable [ind ] = __iterable [ind ], __iterable [index ]
142
152
if key != None :
143
153
compare [index ], compare [ind ] = compare [ind ], compare [index ]
144
- array [index + 1 ], array [r ] = array [r ], array [index + 1 ]
154
+ __iterable [index + 1 ], __iterable [r ] = __iterable [r ], __iterable [index + 1 ]
145
155
if key != None :
146
156
compare [index + 1 ], compare [r ] = compare [r ], compare [index + 1 ]
147
157
return index + 1
148
- solve (0 , len (array )- 1 )
158
+ solve (0 , len (__iterable )- 1 )
159
+ return __iterable
149
160
150
- def merge (array : Iterable [_T ], key : Optional [Callable [[_T ], SupportsRichComparison ]]= None , reverse : bool = False ) -> None :
161
+ def merge (__iterable : Iterable [_T ], key : Optional [Callable [[_T ], SupportsRichComparison ]]= None , reverse : bool = False ) -> list :
151
162
'''
152
- :param array : iterable data.
163
+ :param __iterable : iterable data.
153
164
:param key: callable function, for example: key=lambda x: x[1], key=lambda x: (x[0], x[1]).
154
165
:param reverse: whether to use descending order. The default is ascending order.
155
166
'''
156
- compare = generate (array , key )
157
- def merge (low : int , mid : int , high : int ) -> None :
167
+ __iterable = convert (__iterable )
168
+ compare = generate (__iterable , key )
169
+ def merg (low : int , mid : int , high : int ) -> None :
158
170
'''
159
- :param low: The low-side cursor of array (int).
160
- :param mid: The middle-side cursor of array (int).
161
- :param high: The high-side cursor of array (int).
171
+ :param low: The low-side cursor of __iterable (int).
172
+ :param mid: The middle-side cursor of __iterable (int).
173
+ :param high: The high-side cursor of __iterable (int).
162
174
'''
163
- left , lc = array [low : mid ], compare [low : mid ]
164
- right , rc = array [mid : high ], compare [mid : high ]
175
+ left , lc = __iterable [low : mid ], compare [low : mid ]
176
+ right , rc = __iterable [mid : high ], compare [mid : high ]
165
177
i = 0
166
178
j = 0
167
179
result , store = [], []
@@ -178,23 +190,24 @@ def merge(low: int, mid: int, high: int) -> None:
178
190
store += lc [i :]
179
191
result += right [j :]
180
192
store += rc [j :]
181
- array [low : high ] = result
193
+ __iterable [low : high ] = result
182
194
compare [low : high ] = store
183
195
184
196
def solve () -> None :
185
197
'''
186
198
main
187
199
'''
188
200
i = 1
189
- while i < len (array ):
201
+ while i < len (__iterable ):
190
202
low = 0
191
- while low < len (array ):
203
+ while low < len (__iterable ):
192
204
mid = low + i
193
- high = min (low + 2 * i , len (array ))
205
+ high = min (low + 2 * i , len (__iterable ))
194
206
if mid < high :
195
- merge (low , mid , high )
207
+ merg (low , mid , high )
196
208
low += 2 * i
197
209
i *= 2
198
210
solve ()
211
+ return __iterable
199
212
200
213
__all__ = [bubble , insert , shell , heap , quick , merge ]
0 commit comments