forked from Jack-Lee-Hiter/AlgorithmsByPython
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRadixSort.py
More file actions
Latest commit
33 lines (29 loc) · 913 Bytes
/
Copy pathRadixSort.py
File metadata and controls
33 lines (29 loc) · 913 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
'''
实现基数排序RadixSort, 分为:
最高位优先(Most Significant Digit first)法
最低位优先(Least Significant Digit first)法
'''
# 最低位优先法
defradixSortLSD(alist):
iflen(alist) ==0:
return
iflen(alist) ==1:
returnalist
tempList=alist
maxNum=max(alist)
radix=10
whilemaxNum*10>radix:
newArr= [[], [], [], [], [], [], [], [], [], []]
forn1intempList:
testnum=n1%radix
testnum=testnum// (radix/10)
forn2inrange(10):
iftestnum==n2:
newArr[n2].append(n1)
tempList= []
foriinrange(len(newArr)):
forjinrange(len(newArr[i])):
tempList.append(newArr[i][j])
radix*=10
returntempList
print(radixSortLSD([10, 12, 24, 23, 13, 52, 15, 158, 74, 32, 254, 201, 30, 19]))