Skip to content

Commit f5fec12

Browse files
InnoFangkeon
authored andcommitted
fix sort/radix_sort (keon#395) (keon#397)
* fix sort/radix_sort (keon#395) * Add a new line for radix_sort.py
1 parent cdae90c commit f5fec12

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

algorithms/sort/radix_sort.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
"""
22
radix sort
3-
complexity: O(nk) . n is the size of input list and k is the digit length of the number
3+
complexity: O(nk + n) . n is the size of input list and k is the digit length of the number
44
"""
55
def radix_sort(arr, simulation=False):
6-
is_done = False
76
position = 1
7+
max_number = max(arr)
88

99
iteration = 0
1010
if simulation:
11-
print("iteration",iteration,":",*arr)
11+
print("iteration", iteration, ":", *arr)
1212

13-
while not is_done:
13+
while position < max_number:
1414
queue_list = [list() for _ in range(10)]
15-
is_done = True
1615

1716
for num in arr:
1817
digit_number = num // position % 10
1918
queue_list[digit_number].append(num)
20-
if is_done and digit_number > 0:
21-
is_done = False
2219

2320
index = 0
2421
for numbers in queue_list:
@@ -28,7 +25,8 @@ def radix_sort(arr, simulation=False):
2825

2926
if simulation:
3027
iteration = iteration + 1
31-
print("iteration",iteration,":",*arr)
28+
print("iteration", iteration, ":", *arr)
3229

3330
position *= 10
3431
return arr
32+

0 commit comments

Comments
 (0)