1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15- from ._utils import core , generate
15+ from ._utils import core , generate , convert
1616from ._typing import Iterable , Callable , Optional , _T , SupportsRichComparison
1717
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 :
1919 '''
20- :param array : iterable data.
20+ :param __iterable : iterable data.
2121 :param key: callable function, for example: key=lambda x: x[1], key=lambda x: (x[0], x[1]).
2222 :param reverse: whether to use descending order. The default is ascending order.
2323 '''
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 ):
2627 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 ):
2829 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 ]
3031 flag = True
3132 if key != None :
3233 compare [j ], compare [j + 1 ] = compare [j + 1 ], compare [j ]
3334 if not flag :
3435 break
36+ return __iterable
3537
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 :
3739 '''
38- :param array : iterable data.
40+ :param __iterable : iterable data.
3941 :param key: callable function, for example: key=lambda x: x[1], key=lambda x: (x[0], x[1]).
4042 :param reverse: whether to use descending order. The default is ascending order.
4143 '''
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 ]
4548 low , high = 0 , index - 1
4649 while low <= high : # sequence conforming to monotonicity
4750 mid = (low + high ) // 2
@@ -50,45 +53,50 @@ def insert(array: Iterable[_T], key: Optional[Callable[[_T], SupportsRichCompari
5053 else :
5154 high = mid - 1
5255 for pre in range (index , low , - 1 ): # from back to front
53- array [pre ] = array [pre - 1 ]
56+ __iterable [pre ] = __iterable [pre - 1 ]
5457 if key != None :
5558 compare [pre ] = compare [pre - 1 ]
56- array [low ] = keya
59+ __iterable [low ] = keya
5760 if key != None :
5861 compare [low ] = keyc
62+ return __iterable
5963
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 :
6165 '''
62- :param array : iterable data.
66+ :param __iterable : iterable data.
6367 :param key: callable function, for example: key=lambda x: x[1], key=lambda x: (x[0], x[1]).
6468 :param reverse: whether to use descending order. The default is ascending order.
6569 '''
66- compare = generate (array , key )
67- length = len (array )
70+ __iterable = convert (__iterable )
71+ compare = generate (__iterable , key )
72+ length = len (__iterable )
6873 gap = 1
6974 while gap < length / 3 :
7075 gap = int (3 * gap + 1 )
7176 while gap >= 1 :
7277 for index in range (gap , length ):
7378 next = index
7479 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 ]
7681 if key != None :
7782 compare [next ], compare [next - gap ] = compare [next - gap ], compare [next ]
7883 next -= gap
7984 gap = int (gap / 3 )
85+ return __iterable
86+
8087
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 :
8289 '''
83- :param array : iterable data.
90+ :param __iterable : iterable data.
8491 :param key: callable function, for example: key=lambda x: x[1], key=lambda x: (x[0], x[1]).
8592 :param reverse: whether to use descending order. The default is ascending order.
8693 '''
87- compare = generate (array , key )
94+ __iterable = convert (__iterable )
95+ compare = generate (__iterable , key )
8896 def build (root : int , end : int ) -> None :
8997 '''
9098 :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).
92100 '''
93101 piv = root
94102 left = 2 * root + 1
@@ -98,27 +106,29 @@ def build(root: int, end: int) -> None:
98106 if right < end and core (compare [right ], compare [piv ], key , reverse ):
99107 piv = right
100108 if piv != root :
101- array [root ], array [piv ] = array [piv ], array [root ]
109+ __iterable [root ], __iterable [piv ] = __iterable [piv ], __iterable [root ]
102110 if key != None :
103111 compare [root ], compare [piv ] = compare [piv ], compare [root ]
104112 build (piv , end )
105113
106- length = len (array )
114+ length = len (__iterable )
107115 for root in range (length // 2 - 1 , - 1 , - 1 ):
108116 build (root , length )
109117 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 ]
111119 if key != None :
112120 compare [0 ], compare [end ] = compare [end ], compare [0 ]
113121 build (0 , end )
122+ return __iterable
114123
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 :
116125 '''
117- :param array : iterable data.
126+ :param __iterable : iterable data.
118127 :param key: callable function, for example: key=lambda x: x[1], key=lambda x: (x[0], x[1]).
119128 :param reverse: whether to use descending order. The default is ascending order.
120129 '''
121- compare = generate (array , key )
130+ __iterable = convert (__iterable )
131+ compare = generate (__iterable , key )
122132 def solve (l : int , r : int ) -> None :
123133 '''
124134 main
@@ -130,38 +140,40 @@ def solve(l: int, r: int) -> None:
130140
131141 def partition (l : int , r : int ) -> int :
132142 '''
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).
135145 '''
136146 val = compare [r ]
137147 index = l - 1
138148 for ind in range (l , r ):
139149 if core (val , compare [ind ], key , reverse ):
140150 index += 1
141- array [index ], array [ind ] = array [ind ], array [index ]
151+ __iterable [index ], __iterable [ind ] = __iterable [ind ], __iterable [index ]
142152 if key != None :
143153 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 ]
145155 if key != None :
146156 compare [index + 1 ], compare [r ] = compare [r ], compare [index + 1 ]
147157 return index + 1
148- solve (0 , len (array )- 1 )
158+ solve (0 , len (__iterable )- 1 )
159+ return __iterable
149160
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 :
151162 '''
152- :param array : iterable data.
163+ :param __iterable : iterable data.
153164 :param key: callable function, for example: key=lambda x: x[1], key=lambda x: (x[0], x[1]).
154165 :param reverse: whether to use descending order. The default is ascending order.
155166 '''
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 :
158170 '''
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).
162174 '''
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 ]
165177 i = 0
166178 j = 0
167179 result , store = [], []
@@ -178,23 +190,24 @@ def merge(low: int, mid: int, high: int) -> None:
178190 store += lc [i :]
179191 result += right [j :]
180192 store += rc [j :]
181- array [low : high ] = result
193+ __iterable [low : high ] = result
182194 compare [low : high ] = store
183195
184196 def solve () -> None :
185197 '''
186198 main
187199 '''
188200 i = 1
189- while i < len (array ):
201+ while i < len (__iterable ):
190202 low = 0
191- while low < len (array ):
203+ while low < len (__iterable ):
192204 mid = low + i
193- high = min (low + 2 * i , len (array ))
205+ high = min (low + 2 * i , len (__iterable ))
194206 if mid < high :
195- merge (low , mid , high )
207+ merg (low , mid , high )
196208 low += 2 * i
197209 i *= 2
198210 solve ()
211+ return __iterable
199212
200213__all__ = [bubble , insert , shell , heap , quick , merge ]
0 commit comments