-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsortari.py
247 lines (220 loc) · 5.67 KB
/
sortari.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import random
import time
import tracemalloc
x=int(input())
y=int(input())
L1=[random.randint(0,x) for i in range(y)]
'''L=[int(x) for x in input().split()]'''
L2 = L1.copy()
L3 = L1.copy()
L4 = L1.copy()
L5 = L1.copy()
L6 = L1.copy()
L7 = L1.copy()
L8 = L1.copy()
def is_sorted(L):
for i in range(len(L)-1):
if L[i] > L[i+1]:
return False
return True
def InsertionSort(L):
if len(L) > 10**6 - 1:
print("Nu poate sorta rapid!")
return L
for i in range(1,len(L)):
j = i-1
key = L[i]
while j != -1 and L[j] > key:
L[j+1] = L[j]
j -= 1
L[j+1] = key
def CountSort(L):
fr = [0]*(max(L)+1)
for x in L:
fr[x] += 1
Lt = []
for i in range(1, len(fr)):
while fr[i] != 0:
Lt.append(i)
fr[i] -= 1
return Lt
def merge(Lista, st, mij, dr):
a = mij - st + 1
b = dr - mij
L = [0]*a
R = [0]*b
for i in range(a):
L[i] = Lista[st + i]
for j in range(b):
R[j] = Lista[mij + 1 + j]
i = 0
j = 0
k = st
while i < a and j < b:
if L[i] < R[j]:
Lista[k] = L[i]
i += 1
else:
Lista[k] = R[j]
j += 1
k += 1
while i < a:
Lista[k] = L[i]
i += 1
k += 1
while j < b:
Lista[k] = R[j]
j += 1
k += 1
def mergeSort(Lista, st, dr):
if st < dr:
mij = st + (dr - st) // 2
mergeSort(Lista, st, mij)
mergeSort(Lista, mij + 1, dr)
merge(Lista, st, mij, dr)
def RadixSort(L):
nrmax = max(L)
bucket = [0] * 10
cop = [0] * len(L)
p = 1
while nrmax // p > 0:
bucket = [0] * 10
for i in range(len(L)):
cifra = (L[i] // p) % 10
bucket[cifra] += 1
for i in range(1, 10):
bucket[i] += bucket[i - 1]
for i in range(len(L) - 1, -1, -1):
cifra = (L[i] // p) % 10
cop[bucket[cifra] - 1] = L[i]
bucket[cifra] -= 1
for i in range(len(L)):
L[i] = cop[i]
p *= 10
def RadixSort16(L):
nrmax = max(L)
bucket = [0] * 16
cop = [0] * len(L)
p = 1
while nrmax // p > 0:
bucket = [0] * 16
for i in range(len(L)):
cifra = (L[i] // p) % 16
bucket[cifra] += 1
for i in range(1, 16):
bucket[i] += bucket[i - 1]
for i in range(len(L) - 1, -1, -1):
cifra = (L[i] // p) % 16
cop[bucket[cifra] - 1] = L[i]
bucket[cifra] -= 1
for i in range(len(L)):
L[i] = cop[i]
p *= 16
def RadixSort216(L):
nrmax = max(L)
bucket = [0] * 2**16
cop = [0] * len(L)
p = 1
while nrmax // p > 0:
bucket = [0] * 2**16
for i in range(len(L)):
cifra = (L[i] // p) % 2**16
bucket[cifra] += 1
for i in range(1, 2**16):
bucket[i] += bucket[i - 1]
for i in range(len(L) - 1, -1, -1):
cifra = (L[i] // p) % 2**16
cop[bucket[cifra] - 1] = L[i]
bucket[cifra] -= 1
for i in range(len(L)):
L[i] = cop[i]
p *= 2**16
def ShellSort(L):
n = len(L)
if len(L) > 10**7-1:
print("Nu poate sorta rapid!")
return L
gap = n // 2
while gap > 0:
for i in range(gap, n):
tmp = L[i]
j = i
while j >= gap and L[j - gap] > tmp:
L[j] = L[j - gap]
j -= gap
L[j] = tmp
gap //= 2
tracemalloc.start()
start_time=time.time()
InsertionSort(L1)
end_time=time.time()
print(f"InsertionSort elapsed time: {end_time - start_time} seconds")
print(f"Memory used: {tracemalloc.get_traced_memory()}")
print(is_sorted(L1))
tracemalloc.stop()
tracemalloc.start()
start_time=time.time()
L2=CountSort(L2)
end_time=time.time()
print(f"CountSort elapsed time: {end_time - start_time} seconds")
print(f"Memory used: {tracemalloc.get_traced_memory()}")
print(is_sorted(L2))
tracemalloc.stop()
tracemalloc.start()
start_time=time.time()
mergeSort(L3,0,len(L3)-1)
end_time=time.time()
print(f"Mergesort elapsed time: {end_time - start_time} seconds")
print(f"Memory used: {tracemalloc.get_traced_memory()}")
print(is_sorted(L3))
tracemalloc.stop()
tracemalloc.start()
start_time=time.time()
RadixSort(L4)
end_time=time.time()
print(f"Radixsort elapsed time: {end_time - start_time} seconds")
print(f"Memory used: {tracemalloc.get_traced_memory()}")
print(is_sorted(L4))
tracemalloc.stop()
tracemalloc.start()
start_time=time.time()
RadixSort16(L5)
end_time=time.time()
print(f"Radixsort16 elapsed time: {end_time - start_time} seconds")
print(f"Memory used: {tracemalloc.get_traced_memory()}")
print(is_sorted(L5))
tracemalloc.stop()
tracemalloc.start()
start_time=time.time()
RadixSort216(L6)
end_time=time.time()
print(f"Radixsort216 elapsed time: {end_time - start_time} seconds")
print(f"Memory used: {tracemalloc.get_traced_memory()}")
print(is_sorted(L6))
tracemalloc.stop()
tracemalloc.start()
start_time=time.time()
ShellSort(L7)
end_time=time.time()
print(f"ShellSort elapsed time: {end_time - start_time} seconds")
print(f"Memory used: {tracemalloc.get_traced_memory()}")
print(is_sorted(L7))
tracemalloc.stop()
tracemalloc.start()
start_time=time.time()
L8=sorted(L8)
end_time=time.time()
print(f"Native sort elapsed time: {end_time - start_time} seconds")
print(f"Memory used: {tracemalloc.get_traced_memory()}")
print(is_sorted(L8))
tracemalloc.stop()
'''i=0
with open("printare","w") as g:
for x in L:
g.write(str(x) + " ")
if i == 9:
g.write('\n')
i=0
else:
i+=1
g.close()'''