Sei sulla pagina 1di 4

from random import seed

from random import randint


import time

class Student(object):
def __init__(self, nume, facultate):
self.nume = nume
self.facultate = facultate
def __str__(self):
return "{} {}".format(self.nume,self.facultate)

def generate_alphabetic_pattern():
v = randint(5,20)
# s = str()
# lst = []
w = 65
n = 0
with open("studenti_alfabetic.txt",'w') as f:
for i in range(1000000):
s = str()
n+=1
s+=chr(w)
for j in range(v):
c = randint(97,122)
s+=chr(c)
f.write(s+'\n')
if n%10000 == 0:
w+=1
if w == 90:
w = 90

def generate_alphabetic_desc_pattern():
v = randint(5,20)
# s = str()
# lst = []
w = 90
n = 0
with open("studenti_alfabetic_desc.txt",'w') as f:
for i in range(1000000):
s = str()
n+=1
s+=chr(w)
for j in range(v):
c = randint(97,122)
s+=chr(c)
f.write(s+'\n')
if n%10000 == 0:
w-=1
if w == 66:
w = 66

def generate_pattern():
v = randint(5,20)
s = str()
w = randint(65,90)
s += chr(w)
for i in range(v):
w = randint(97,122)
s+=chr(w)
return s

facultati = ["Litere",
"Psihologie",
"Drept",
"Muzica",
"Matematica Informatica",
"Arte",
"Arhitectura",
"Geografie",
"Biologie Chimie",
"Educatie Fizica"]

def constr_fisier_facultati():
with open("facultati.txt",'w') as f:
for i in range(1000000):
v = randint(0,len(facultati))
f.write(facultati[i]+'\n')

def constr_fisier_studenti():
with open("studenti.txt",'w') as g:
for i in range(1000000):
g.write(generate_pattern()+'\n')

#constr_fisier_facultati()
#constr_fisier_studenti()

def incarc_date(name):
lst = []
with open("facultati.txt",'r') as f:
l = f.readline()
while l:
S = Student(None,l.rstrip("\n"))
lst.append(S)
l = f.readline()
i = 0
with open(name,'r') as g:
l = g.readline()
while l:
lst[i].nume = l.rstrip("\n")
i+=1
l = g.readline()
#for i in range(10):
# print(lst[i],end=' ')
return lst

def get_dict(studs):
d = dict()
for el in facultati:
d[el] = []
for el in studs:
d[str(el.facultate)].append(el.nume)
# print(d["Muzica"])
return d

def insertion_sort(studs,l):
# Traverse through 1 to len(arr)
for i in range(1,l):
key = studs[i]
j = i-1
while j >=0 and key.nume < studs[j].nume :
studs[j+1] = studs[j]
j -= 1
studs[j+1] = key
return studs

def bubble_sort(studs,l):
for i in range(l-1):
for j in range(i+1,l):
if studs[i].nume > studs[j].nume:
(studs[i],studs[j])=(studs[j],studs[i])
return studs

def selection_sort(studs,l):
for i in range(l):
min_idx = i
for j in range(i+1, l):
if studs[min_idx].nume > studs[j].nume:
min_idx = j
(studs[i], studs[min_idx]) = (studs[min_idx], studs[i])
return studs

def quick_sort(studs,pi,ps):
a, b=pi, ps
mij=(a+b)//2
while a<=b:
while studs[a].nume<studs[mij].nume:
a=a+1
while studs[b].nume>studs[mij].nume:
b=b-1
if a<=b:
aux=studs[a]
studs[a]=studs[b]
studs[b]=aux
a=a+1
b=b-1
if a<ps:
quick_sort(studs,a,ps)
if b>pi:
quick_sort(studs,pi,b)
return studs

l = 1000

def mergeSort(studs):
if len(studs)>1:
mid = len(studs)//2 #Finding the mid of the array
L = studs[:mid] # Dividing the array elements
R = studs[mid:] # into 2 halves
mergeSort(L) # Sorting the first half
mergeSort(R) # Sorting the second half
i = j = k = 0
# Copy data to temp arrays L[] and R[]
while i < len(L) and j < len(R):
if L[i].nume is not None and R[j].nume is not None:
if L[i].nume < R[j].nume:
studs[k] = L[i]
i+=1
else:
studs[k] = R[j]
j+=1
k+=1
# Checking if any element was left
while i < len(L):
studs[k] = L[i]
i+=1
k+=1

while j < len(R):


studs[k] = R[j]
j+=1
k+=1

print("start")
#generate_alphabetic_pattern()
#generate_alphabetic_desc_pattern()
print("Am incarcat date")
print("Sortam...")
L = 10
while L<=100000:
print(L)
l = incarc_date("studenti_alfabetic_desc.txt")
for i in range(5):
S=0
k=0
start = time.time()
insertion_sort(l,L)
end = time.time()
print('Test {} : {}'.format(i,float(end-start)))
if float(end-start):
S+=float(end-start)
k+=1
if k:
print('In medie: ',float(S/k))
else:
print('In medie: 0.0')
L*=10