-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCountingSort.py
More file actions
75 lines (46 loc) · 1.17 KB
/
CountingSort.py
File metadata and controls
75 lines (46 loc) · 1.17 KB
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
def find_min_max(items):
min = items[0]
max = items[0]
for n in items:
if n > max:
max = n
if n < min:
min = n;
return [min,max]
def get_position(items, n):
result = None
if items[0] == n:
result = 0
else:
diff = n - items[0]
result = diff
return result
def sort(items):
result = find_min_max(items)
min = result[0]
max = result[1]
index = []
for n in range(min,max + 1):
index.append(n)
count = []
for i in range(len(index)):
count.append(0)
for n in items:
i = get_position(index, n)
count[i] = count[i] + 1
sum_count = []
sum_count.append(count[0])
for i in range(1, len(count)):
sum_count.append(count[i] + sum_count[i - 1])
result = []
for i in range(len(items)):
result.append(0)
for n in items:
position = get_position(index, n)
j = sum_count[position] - 1
result[j] = n
return result
if __name__ == '__main__':
sequencia = [10,7,12,4,9,13]
print("seqInicial: " + str(sequencia))
print("seqFinal: " + str(sort(sequencia)))