-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCounting sort.py
84 lines (72 loc) · 1.52 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# counting sort
# Bu algoritm yalnız müsbət tam ədədləri və aralıqları məhdud olan ədədləri sıralamaq üçün effektivdir
lst = [4, 2, 2, 8, 3, 3, 1]
# Siyahıdakı ən böyük elementi tapırıq
max_value = max(lst)
# Sayma siyahısını (count array) qururuq
count = [0] * (max_value + 1)
# Hər elementin sayını tapırıq
for num in lst:
count[num] += 1
# Sıralanmış siyahını yaradaraq doldururuq
sorted_lst = []
for i in range(len(count)):
sorted_lst.extend([i] * count[i]) # Hər elementi sayına uyğun əlavə edirik
print(sorted_lst)
# counting sort alqoritmi II method ________________________________________________________________________
A=[2,2,6,1,6,2,2]
maks=max(A)
say=[0]*(maks+1)
for i in range(len(A)):
say[A[i]]+=1
"""
say=[0,0,0,0,0,0,0]
say[2]=1
say[2]=2
say[6]=1
say[1]=1
say[6]=2
say[2]=3
say[2]=4
--------------------------------------------
0 1 2 3 4 5 6 -> elementleri
say=[0,1,4,0,0,0,2] -> elementlerin saylari
"""
for i in range(1,maks+1):
say[i]+=say[i-1]
"""
0,1,4,0,0,0,2
--------------
1,4,0,0,0,2
--------------
1,5,0,0,0,2
--------------
1,5,5,0,0,2
--------------
1,5,5,5,0,2
--------------
1,5,5,5,5,2
--------------
1,5,5,5,5,7
"""
B=[0]*len(A)
for i in range(len(A)-1,-1,-1):
B[say[A[i]]-1] = A[i]
say[A[i]]-=1
"""
----------------------
say[1]=1 -> 0
say[2]=5 -> 4, 3, 2, 1
say[6]=7 -> 6, 5
2 -> B[4]=2
2 -> B[3]=2
6 -> B[6]=6
1 -> B[0]=1
6 -> B[5]=6
2 -> B[2]=2
2 -> B[1]=2
B=[1,2,2,2,2,6,6]
-----------------------
"""
A=B
print(A)