@@ -67,11 +67,9 @@ def _find_run(
6767 if start >= end - 1 :
6868 return start + 1
6969
70-
7170 key_func = key if key else lambda x : x
7271 run_end = start + 1
7372
74-
7573 # Check if run is ascending or descending
7674 if key_func (arr [run_end ]) < key_func (arr [start ]):
7775 # Descending run
@@ -84,7 +82,6 @@ def _find_run(
8482 while run_end < end and key_func (arr [run_end ]) >= key_func (arr [run_end - 1 ]):
8583 run_end += 1
8684
87-
8885 return run_end
8986
9087
@@ -133,7 +130,6 @@ def _node_power(n: int, b1: int, n1: int, b2: int, n2: int) -> int:
133130 while (a * (1 << power )) // two_n == (b * (1 << power )) // two_n :
134131 power += 1
135132
136-
137133 return power
138134
139135
@@ -170,16 +166,13 @@ def _merge(
170166 """
171167 key_func = key if key else lambda x : x
172168
173-
174169 # Copy the runs to temporary storage
175170 left = arr [start1 :end1 ]
176171 right = arr [end1 :end2 ]
177172
178-
179173 i = j = 0
180174 k = start1
181175
182-
183176 # Merge the two runs
184177 while i < len (left ) and j < len (right ):
185178 if key_func (left [i ]) <= key_func (right [j ]):
@@ -190,14 +183,12 @@ def _merge(
190183 j += 1
191184 k += 1
192185
193-
194186 # Copy remaining elements
195187 while i < len (left ):
196188 arr [k ] = left [i ]
197189 i += 1
198190 k += 1
199191
200-
201192 while j < len (right ):
202193 arr [k ] = right [j ]
203194 j += 1
@@ -269,12 +260,10 @@ def power_sort(
269260 if len (collection ) <= 1 :
270261 return collection
271262
272-
273263 # Make a copy to avoid modifying the original if it's immutable
274264 arr = list (collection )
275265 n = len (arr )
276266
277-
278267 # Adjust key function for reverse sorting
279268 needs_final_reverse = False
280269 if reverse :
@@ -287,7 +276,6 @@ def reverse_key(x):
287276 return - val
288277 return val
289278
290-
291279 key = reverse_key
292280 needs_final_reverse = True
293281 else :
@@ -303,34 +291,29 @@ def reverse_cmp(x):
303291 # Stack to hold runs: each entry is (start_index, length, power)
304292 stack : list [tuple [int , int , int ]] = []
305293
306-
307294 start = 0
308295 while start < n :
309296 # Find the next run
310297 run_end = _find_run (arr , start , n , key )
311298 run_length = run_end - start
312299
313-
314300 # Calculate power for this run
315301 if len (stack ) == 0 :
316302 power = 0
317303 else :
318304 prev_start , prev_length , _ = stack [- 1 ]
319305 power = _node_power (n , prev_start , prev_length , start , run_length )
320306
321-
322307 # Merge runs from stack based on power comparison
323308 while len (stack ) > 0 and stack [- 1 ][2 ] >= power :
324309 # Merge the top run with the current run
325310 prev_start , prev_length , _ = stack .pop ()
326311 _merge (arr , prev_start , prev_start + prev_length , run_end , key )
327312
328-
329313 # Update current run to include the merged run
330314 start = prev_start
331315 run_length = run_end - start
332316
333-
334317 # Recalculate power
335318 if len (stack ) == 0 :
336319 power = 0
@@ -340,19 +323,16 @@ def reverse_cmp(x):
340323 n , prev_prev_start , prev_prev_length , start , run_length
341324 )
342325
343-
344326 # Push current run onto stack
345327 stack .append ((start , run_length , power ))
346328 start = run_end
347329
348-
349330 # Merge all remaining runs on the stack
350331 while len (stack ) > 1 :
351332 start2 , length2 , _ = stack .pop ()
352333 start1 , length1 , _ = stack .pop ()
353334 _merge (arr , start1 , start1 + length1 , start2 + length2 , key )
354335
355-
356336 # Recalculate power for merged run
357337 if len (stack ) == 0 :
358338 power = 0
@@ -363,7 +343,6 @@ def reverse_cmp(x):
363343
364344 stack .append ((start1 , start2 + length2 - start1 , power ))
365345
366-
367346 # Handle reverse sorting for non-numeric types
368347 if (
369348 reverse
@@ -382,34 +361,27 @@ def reverse_cmp(x):
382361if __name__ == "__main__" :
383362 import doctest
384363
385-
386364 doctest .testmod ()
387365
388-
389366 print ("\n PowerSort Interactive Testing" )
390367 print ("=" * 40 )
391368
392-
393369 try :
394370 user_input = input ("Enter numbers separated by a comma:\n " ).strip ()
395371 if user_input == "" :
396372 unsorted = []
397373 else :
398374 unsorted = [int (item .strip ()) for item in user_input .split ("," )]
399375
400-
401376 print (f"\n Original: { unsorted } " )
402377 sorted_list = power_sort (unsorted )
403378 print (f"Sorted: { sorted_list } " )
404379
405-
406380 # Test reverse
407381 sorted_reverse = power_sort (unsorted , reverse = True )
408382 print (f"Reverse: { sorted_reverse } " )
409383
410-
411384 except ValueError :
412385 print ("Invalid input. Please enter valid integers separated by commas." )
413386 except KeyboardInterrupt :
414387 print ("\n \n Goodbye!" )
415-
0 commit comments