-
Notifications
You must be signed in to change notification settings - Fork 1
/
counting_sort.py
66 lines (53 loc) · 1.59 KB
/
counting_sort.py
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os, sys, warnings, random
def counting_sort(item, begin, end, key=None):
if key is None:
key = lambda x: x[0] if isinstance(x, (list, tuple)) else x
mn, mx = sys.maxsize, -sys.maxsize
i=begin
'''finding min and max element'''
while i+1 <= end:
if key(item[i]) < key(item[i+1]):
mn = key(item[i]) if key(item[i]) < mn else mn
mx = key(item[i+1]) if key(item[i+1]) > mx else mx
else:
mn = key(item[i+1]) if key(item[i+1]) < mn else mn
mx = key(item[i]) if key(item[i]) > mx else mx
i+=2
if key(item[end]) < mn:
mn = key(item[end])
if key(item[end]) > mx:
mx=key(item[end])
pos = [0]*(mx-mn+1)
n=len(item)
sorted_ = [None]*n
i=0
while i<begin:
sorted_[i]=item[i]
i+=1
i=end+1
while i<n:
sorted_[i]=item[i]
i+=1
for e in item[begin: end+1]:
pos[key(e)-mn]+=1
for i in range(1, len(pos)):
pos[i] += pos[i-1]
i=end
while i>=begin:
sorted_[ begin+pos[key(item[i])-mn]-1 ] = item[i]
pos[key(item[i])-mn] -= 1
i-=1
return sorted_
if __name__=='__main__':
item = list(range(100, -30, -2))
sort_item = counting_sort(item, 0, len(item)-1)
print(sort_item, end='\n'*2)
more=True
if more:
item1=[(21, 14),
(31, 132),
(131, 12),
(1231, 1),
(131, 13)]
sort2=counting_sort(item1, 0, len(item1)-1, key=lambda x: x[0])
print(sort2)