[1] initial commit

This commit is contained in:
osininyai 2026-05-21 13:43:45 +03:00
parent 27e1e98bf4
commit f89f54eddc
9 changed files with 6379 additions and 0 deletions

383
osininyai/[1]MP_BST.py Normal file
View File

@ -0,0 +1,383 @@
from MP_records import records
import random as rd
import time
import csv
import codecs
import sys
sys.setrecursionlimit(15000)
def bst_insert(root,name,phone):
if root==None:
entry={"name":name,"phone":phone,"left":None,"right":None}
root=entry
return root
else:
entry={"name":name,"phone":phone,"left":None,"right":None}
if root["phone"]==phone:
root["name"]=name
return root
else:
if (name.encode())<(root["name"].encode()):
if root["left"]==None:
root["left"]=entry
else:
bst_insert(root["left"], name, phone)
else:
if root["right"]==None:
root["right"]=entry
else:
bst_insert(root["right"], name, phone)
def bst_find(root, name):
a=None
if root!=None:
if root["name"]==name:
return root["phone"]
else:
if (name.encode())>(root["name"].encode()) and root["right"]:
a=bst_find(root["right"], name)
elif root["left"] and a==None:
a=bst_find(root["left"], name)
return a
def bst_delete(root,name):
while root["name"]!=name:
if (name.encode())>(root["name"].encode()):
if root["right"]==None:
#print("None")
return
else:
if root["right"]["name"]==name and root["right"]["right"]==None and root["right"]["left"]==None:
root["right"]=None
return
root=root["right"]
else:
if root["left"]==None:
#print("None")
return
else:
if root["left"]["name"]==name and root["left"]["right"]==None and root["left"]["left"]==None:
root["left"]=None
return
root=root["left"]
if root["right"]:
root1=root["right"]
if root1["left"]:
if root1["left"]["left"]:
while root1["left"]["left"]:
root1=root1["left"]
root2=root1["left"]
else:
root2=root1["left"]
else:
root["name"]=root1["name"]
root["phone"]=root1["phone"]
root["right"]=root1["right"]
return
if root2["right"]:
root["name"]=root2["name"]
root["phone"]=root2["phone"]
root1["left"]=root2["right"]
return
#del root2
else:
root["name"]=root2["name"]
root["phone"]=root2["phone"]
root1["left"]=None
#print(root1.right.data)
return
elif root["left"]:
temp=root["left"]["left"]
root["name"]=root["left"]["name"]
root["phone"]=root["left"]["phone"]
root["right"]=root["left"]["right"]
root["left"]=temp
return
def bst_list_all(root):
if root["left"]:
bst_list_all(root["left"])
print(root["name"]," - ",root["phone"])
if root["right"]:
bst_list_all(root["right"])
def test():
root=None
root=bst_insert(root,"Abba",1)
bst_insert(root,"Babba",2)
bst_insert(root,"Cabba",3)
bst_insert(root,"Aaaaa",4)
bst_insert(root,"Abfga",5)
bst_insert(root,"Arte",6)
bst_insert(root,"Aaxa",7)
bst_insert(root,"Aaax",8)
bst_insert(root,"Aaxx",9)
print(root)
print(bst_find(root, "Aaaaa"))
print(bst_find(root, "Aaxx"))
print(bst_find(root, "Aaax"))
print(bst_find(root, "Babba"))
print(bst_find(root, "Cabba"))
print(bst_find(root, "Arte"))
print(bst_find(root, "Aaxa"))
print(bst_find(root, "Abba"))
print(bst_find(root, "Abfga"))
print(bst_find(root, "Abb"))
#bst_delete(root, "Cabba")
print(root)
bst_list_all(root)
def run_shuffled(records_shuffled):
insertion_times=[]
finding_times=[]
deletion_times1=[]
print("Shuffled list: ")
for k in range(5):
lisst=None
#А. Вставка всех записей
start=time.perf_counter()
lisst=bst_insert(lisst, records_shuffled[0][0], records_shuffled[0][1])
for i in range(1,len(records_shuffled)):
bst_insert(lisst, records_shuffled[i][0], records_shuffled[i][1])
end=time.perf_counter()
insertion_times.append(end-start)
#Б. Поиск 100 случайных записей
names=[]
index=rd.randint(0,9899)
for i in range(100):
names.append(records_shuffled[index][0])
index+=1
for i in range(10):
names.append("A")
rd.shuffle(names)
start=time.perf_counter()
for i in range(len(names)):
bst_find(lisst,names[i])
end=time.perf_counter()
finding_times.append(end-start)
#В. Удаление 50 случайных записей
for i in range(10):
names.remove("A")
rd.shuffle(names)
deletion_times=[]
for i in range(50):
start=time.perf_counter()
bst_delete(lisst,names[i])
end=time.perf_counter()
ttt=end-start
deletion_times.append(ttt)
deletion_times1.append(deletion_times)
print("Run number ",k+1)
print("Insertion time: ",insertion_times[k])
print("Finding time: ",finding_times[k])
print("Deletion times: ","\n",deletion_times)
print("\n")
temp=0
for i in range(5):
temp+=insertion_times[i]
temp=temp/5
results = [
[u"Структура", u"Режим", u"Операция", u"Время (сек)"],
["BinarySearchTree", u"случайный", u"вставка", insertion_times[0]],
["BinarySearchTree", u"случайный", u"вставка", insertion_times[1]],
["BinarySearchTree", u"случайный", u"вставка", insertion_times[2]],
["BinarySearchTree", u"случайный", u"вставка", insertion_times[3]],
["BinarySearchTree", u"случайный", u"вставка", insertion_times[4]],
[u"Структура", u"Режим", u"Операция", u"Среднее время (сек)"],
["BinarySearchTree", u"случайный", u"вставка", temp,]
]
with codecs.open("results.csv", "a+", "utf-16") as f:
writer = csv.writer(f)
writer.writerows(results)
writer.writerows("\n")
temp=0
for i in range(5):
temp+=finding_times[i]
temp=temp/5
results = [
[u"Структура", u"Режим", u"Операция", u"Время (сек)"],
["BinarySearchTree", u"случайный", u"поиск", finding_times[0]],
["BinarySearchTree", u"случайный", u"поиск", finding_times[1]],
["BinarySearchTree", u"случайный", u"поиск", finding_times[2]],
["BinarySearchTree", u"случайный", u"поиск", finding_times[3]],
["BinarySearchTree", u"случайный", u"поиск", finding_times[4]],
[u"Структура", u"Режим", u"Операция", u"Среднее время (сек)"],
["BinarySearchTree", u"случайный", u"поиск", temp,]
]
with codecs.open("results.csv", "a+", "utf-16") as f:
writer = csv.writer(f)
writer.writerows(results)
writer.writerows("\n")
temp=0
del_times=[]
for i in range(5):
for j in range(50):
temp+=deletion_times1[i][j]
temp=temp/50
del_times.append(temp)
temp=0
temp=0
for i in range(5):
temp+=del_times[i]
temp=temp/5
results = [
[u"Структура", u"Режим", u"Операция", u"Время (сек)"],
["BinarySearchTree", u"случайный", u"удаление", del_times[0]],
["BinarySearchTree", u"случайный", u"удаление", del_times[1]],
["BinarySearchTree", u"случайный", u"удаление", del_times[2]],
["BinarySearchTree", u"случайный", u"удаление", del_times[3]],
["BinarySearchTree", u"случайный", u"удаление", del_times[4]],
[u"Структура", u"Режим", u"Операция", u"Среднее время (сек)"],
["BinarySearchTree", u"случайный", u"удаление", temp,]
]
with codecs.open("results.csv", "a+", "utf-16") as f:
writer = csv.writer(f)
writer.writerows(results)
writer.writerows("\n")
writer.writerows("\n")
def run_sorted(records_shuffled):
insertion_times=[]
finding_times=[]
deletion_times1=[]
print("Sorted list: ")
for k in range(5):
lisst=None
#А. Вставка всех записей
start=time.perf_counter()
lisst=bst_insert(lisst, records_shuffled[0][0], records_shuffled[0][1])
for i in range(1,len(records_shuffled)):
bst_insert(lisst, records_shuffled[i][0], records_shuffled[i][1])
end=time.perf_counter()
insertion_times.append(end-start)
#Б. Поиск 100 случайных записей
names=[]
index=rd.randint(0,9899)
for i in range(100):
names.append(records_shuffled[index][0])
index+=1
for i in range(10):
names.append("A")
rd.shuffle(names)
start=time.perf_counter()
for i in range(len(names)):
bst_find(lisst,names[i])
end=time.perf_counter()
finding_times.append(end-start)
#В. Удаление 50 случайных записей
for i in range(10):
names.remove("A")
rd.shuffle(names)
deletion_times=[]
for i in range(50):
start=time.perf_counter()
bst_delete(lisst,names[i])
end=time.perf_counter()
ttt=end-start
deletion_times.append(ttt)
deletion_times1.append(deletion_times)
print("Run number ",k+1)
print("Insertion time: ",insertion_times[k])
print("Finding time: ",finding_times[k])
print("Deletion times: ","\n",deletion_times)
print("\n")
temp=0
for i in range(5):
temp+=insertion_times[i]
temp=temp/5
results = [
[u"Структура", u"Режим", u"Операция", u"Время (сек)"],
["BinarySearchTree", u"отсортированный", u"вставка", insertion_times[0]],
["BinarySearchTree", u"отсортированный", u"вставка", insertion_times[1]],
["BinarySearchTree", u"отсортированный", u"вставка", insertion_times[2]],
["BinarySearchTree", u"отсортированный", u"вставка", insertion_times[3]],
["BinarySearchTree", u"сотсортированный", u"вставка", insertion_times[4]],
[u"Структура", u"Режим", u"Операция", u"Среднее время (сек)"],
["BinarySearchTree", u"отсортированный", u"вставка", temp,]
]
with codecs.open("results.csv", "a+", "utf-16") as f:
writer = csv.writer(f)
writer.writerows(results)
writer.writerows("\n")
temp=0
for i in range(5):
temp+=finding_times[i]
temp=temp/5
results = [
[u"Структура", u"Режим", u"Операция", u"Время (сек)"],
["BinarySearchTree", u"отсортированный", u"поиск", finding_times[0]],
["BinarySearchTree", u"отсортированный", u"поиск", finding_times[1]],
["BinarySearchTree", u"отсортированный", u"поиск", finding_times[2]],
["BinarySearchTree", u"отсортированный", u"поиск", finding_times[3]],
["BinarySearchTree", u"отсортированный", u"поиск", finding_times[4]],
[u"Структура", u"Режим", u"Операция", u"Среднее время (сек)"],
["BinarySearchTree", u"отсортированный", u"поиск", temp,]
]
with codecs.open("results.csv", "a+", "utf-16") as f:
writer = csv.writer(f)
writer.writerows(results)
writer.writerows("\n")
temp=0
del_times=[]
for i in range(5):
for j in range(50):
temp+=deletion_times1[i][j]
temp=temp/50
del_times.append(temp)
temp=0
temp=0
for i in range(5):
temp+=del_times[i]
temp=temp/5
results = [
[u"Структура", u"Режим", u"Операция", u"Время (сек)"],
["BinarySearchTree", u"отсортированный", u"удаление", del_times[0]],
["BinarySearchTree", u"отсортированный", u"удаление", del_times[1]],
["BinarySearchTree", u"отсортированный", u"удаление", del_times[2]],
["BinarySearchTree", u"отсортированный", u"удаление", del_times[3]],
["BinarySearchTree", u"отсортированный", u"удаление", del_times[4]],
[u"Структура", u"Режим", u"Операция", u"Среднее время (сек)"],
["BinarySearchTree", u"отсортированный", u"удаление", temp,]
]
with codecs.open("results.csv", "a+", "utf-16") as f:
writer = csv.writer(f)
writer.writerows(results)
writer.writerows("\n")
writer.writerows("\n")
records_shuffled, records_sorted = records()
run_shuffled(records_shuffled)
run_sorted(records_sorted)

View File

@ -0,0 +1,456 @@
from MP_records import records
import string
import random as rd
import time
import csv
import codecs
def polynomial_hash(word):
p=11111
m=(10**9)+9
hashh=0
for i in range(len(word)):
hashh+=ord(word[i])*(p**i)
hashh=hashh%m
return hashh
def hash_to_index(hashh,length):
#print(hashh)
#if len(str(hashh))>4:
#hashh=int(str(hashh)[3:])
while hashh>length:
hashh=hashh%(length)
return hashh
def ll_insert(table,name,phone,index):
if table[index]==None:
entry={"name":name,"phone":phone,"next":None}
table[index]=entry
return table
else:
entry={"name":name,"phone":phone,"next":None}
if table[index]["phone"]==phone:
table[index]["name"]=name
return table
if table[index]["next"]==None:
table[index]["next"]=entry
return table
else:
nexxt=table[index]["next"]
if nexxt["phone"]==phone:
nexxt["name"]=name
return table
while nexxt["next"]!=None:
nexxt=nexxt["next"]
if nexxt["phone"]==phone:
nexxt["name"]=name
return table
nexxt["next"]=entry
return table
def ht_insert(table,name,phone):
index=hash_to_index(polynomial_hash(name), len(table))
ll_insert(table,name,phone,index)
return table
def ht_find(table, name):
index=hash_to_index(polynomial_hash(name), len(table))
if table[index]!=None:
if table[index]["name"]==name:
return table[index]["phone"]
elif table[index]["next"]!=None:
if table[index]["next"]["name"]==name:
return table[index]["next"]["phone"]
else:
nexxt=table[index]["next"]
while nexxt["next"]!=None:
nexxt=nexxt["next"]
if nexxt["name"]==name:
return nexxt["phone"]
return None
def ht_delete(table,name):
index=hash_to_index(polynomial_hash(name), len(table))
if len(table)>0:
if table[index]["name"]==name:
if table[index]["next"]!=None:
table[index]=table[index]["next"]
return table
else:
table[index]=None
return table
elif table[index]["next"]!=None:
if table[index]["next"]["name"]==name:
if table[index]["next"]["next"]!=None:
table[index]["next"]=table[index]["next"]["next"]
return table
else:
table[index]["next"]=None
return table
elif table[index]["next"]["next"]!=None:
nexxt1=table[index]["next"]
nexxt2=nexxt1["next"]
if nexxt2["name"]==name:
if nexxt2["next"]!=None:
nexxt1["next"]=nexxt2["next"]
return table
else:
nexxt1["next"]=None
return table
while nexxt2["next"]!=None:
nexxt1=nexxt2
nexxt2=nexxt1["next"]
if nexxt2["name"]==name:
if nexxt2["next"]!=None:
nexxt1["next"]=nexxt2["next"]
return table
else:
nexxt1["next"]=None
return table
def bad_sort(names,phones):
names1=[]
phones1=[]
while len(names)>0:
min_=names[0].encode()
ph=phones[0]
for i in range(len(names)):
nm=names[i].encode()
if nm<min_:
min_=nm
ph=phones[i]
#print(min_.decode()," - ",ph)
names1.append(min_.decode())
phones1.append(ph)
names.remove(min_.decode())
phones.remove(ph)
#print(names1,"\n",phones1)
return names1, phones1
def Shell(names,phones):
N = len(names)
n = N // 2
while n>0:
for i in range (0,N-n):
j=i
while j+n<N:
if (names[j].encode())>(names[j+n].encode()):
t=names[j]
t1=phones[j]
names[j]=names[j+n]
phones[j]=phones[j+n]
names[j+n]=t
phones[j+n]=t1
j=i
else:
j+=n
n=n//2
return names,phones
def ht_listall(table):
names=[]
phones=[]
pointer=0
while pointer<len(table):
if table[pointer]!=None:
names.append(table[pointer]["name"])
phones.append(table[pointer]["phone"])
if table[pointer]["next"]!=None:
names.append(table[pointer]["next"]["name"])
phones.append(table[pointer]["next"]["phone"])
nexxt=table[pointer]["next"]
while nexxt["next"]!=None:
nexxt=nexxt["next"]
names.append(nexxt["name"])
phones.append(nexxt["phone"])
pointer+=1
names1, phones1 = bad_sort(names, phones)
#names1, phones1 = Shell(names, phones)
for i in range(len(names1)):
print(names1[i]," - ",phones1[i],end='')
if i%4==0:
print("\n")
else:
print(", ",end='')
print("\n")
def test():
table=[]
for i in range(8):
table.append(None)
ht_insert(table, "Zyky", 1)
ht_insert(table, "Abba", 2)
ht_insert(table, "Babba", 3)
ht_insert(table, "Aaaaa", 4)
ht_insert(table, "Aakk", 5)
ht_insert(table, "Bfaw", 6)
ht_insert(table, "Uno", 7)
ht_insert(table, "Uk", 8)
ht_insert(table, "Uaa", 9)
ht_insert(table, "h", 10)
print(table)
print(ht_find(table,"Aakk"))
# ht_delete(table, "Aakk")
#ht_delete(table, "Aaaaa")
#print(table)
#ht_delete(table, "Uaa")
#ht_delete(table, "Zyky")
print(table)
ht_listall(table)
def run_shuffled(records_shuffled):
insertion_times=[]
finding_times=[]
deletion_times1=[]
print("Shuffled list: ")
for k in range(5):
lisst=[]
for i in range(5000):
lisst.append(None)
rd.shuffle(records_shuffled)
#А. Вставка всех записей
start=time.perf_counter()
for i in range(len(records_shuffled)):
ht_insert(lisst, records_shuffled[i][0], records_shuffled[i][1])
end=time.perf_counter()
insertion_times.append(end-start)
#Б. Поиск 100 случайных записей
names=[]
index=rd.randint(0,9899)
for i in range(100):
names.append(records_shuffled[index][0])
index+=1
for i in range(10):
names.append("A")
rd.shuffle(names)
start=time.perf_counter()
for i in range(len(names)):
ht_find(lisst,names[i])
end=time.perf_counter()
finding_times.append(end-start)
#В. Удаление 50 случайных записей
for i in range(10):
names.remove("A")
rd.shuffle(names)
deletion_times=[]
for i in range(50):
start=time.perf_counter()
ht_delete(lisst,names[i])
end=time.perf_counter()
ttt=end-start
deletion_times.append(ttt)
deletion_times1.append(deletion_times)
print("Run number ",k+1)
print("Insertion time: ",insertion_times[k])
print("Finding time: ",finding_times[k])
print("Deletion times: ","\n",deletion_times)
print("\n")
temp=0
for i in range(5):
temp+=insertion_times[i]
temp=temp/5
results = [
[u"Структура", u"Режим", u"Операция", u"Время (сек)"],
["HashTable", u"случайный", u"вставка", insertion_times[0]],
["HashTable", u"случайный", u"вставка", insertion_times[1]],
["HashTable", u"случайный", u"вставка", insertion_times[2]],
["HashTable", u"случайный", u"вставка", insertion_times[3]],
["HashTable", u"случайный", u"вставка", insertion_times[4]],
[u"Структура", u"Режим", u"Операция", u"Среднее время (сек)"],
["HashTable", u"случайный", u"вставка", temp,]
]
with codecs.open("results.csv", "a+", "utf-16") as f:
writer = csv.writer(f)
writer.writerows(results)
writer.writerows("\n")
temp=0
for i in range(5):
temp+=finding_times[i]
temp=temp/5
results = [
[u"Структура", u"Режим", u"Операция", u"Время (сек)"],
["HashTable", u"случайный", u"поиск", finding_times[0]],
["HashTable", u"случайный", u"поиск", finding_times[1]],
["HashTable", u"случайный", u"поиск", finding_times[2]],
["HashTable", u"случайный", u"поиск", finding_times[3]],
["HashTable", u"случайный", u"поиск", finding_times[4]],
[u"Структура", u"Режим", u"Операция", u"Среднее время (сек)"],
["HashTable", u"случайный", u"поиск", temp,]
]
with codecs.open("results.csv", "a+", "utf-16") as f:
writer = csv.writer(f)
writer.writerows(results)
writer.writerows("\n")
temp=0
del_times=[]
for i in range(5):
for j in range(50):
temp+=deletion_times1[i][j]
temp=temp/50
del_times.append(temp)
temp=0
temp=0
for i in range(5):
temp+=del_times[i]
temp=temp/5
results = [
[u"Структура", u"Режим", u"Операция", u"Время (сек)"],
["HashTable", u"случайный", u"удаление", del_times[0]],
["HashTable", u"случайный", u"удаление", del_times[1]],
["HashTable", u"случайный", u"удаление", del_times[2]],
["HashTable", u"случайный", u"удаление", del_times[3]],
["HashTable", u"случайный", u"удаление", del_times[4]],
[u"Структура", u"Режим", u"Операция", u"Среднее время (сек)"],
["HashTable", u"случайный", u"удаление", temp,]
]
with codecs.open("results.csv", "a+", "utf-16") as f:
writer = csv.writer(f)
writer.writerows(results)
writer.writerows("\n")
writer.writerows("\n")
def run_sorted(records_shuffled):
insertion_times=[]
finding_times=[]
deletion_times1=[]
print("Sorted list: ")
for k in range(5):
lisst=[]
for i in range(5000):
lisst.append(None)
#А. Вставка всех записей
start=time.perf_counter()
for i in range(len(records_shuffled)):
ht_insert(lisst, records_shuffled[i][0], records_shuffled[i][1])
end=time.perf_counter()
insertion_times.append(end-start)
#Б. Поиск 100 случайных записей
names=[]
index=rd.randint(0,9899)
for i in range(100):
names.append(records_shuffled[index][0])
index+=1
for i in range(10):
names.append("A")
rd.shuffle(names)
start=time.perf_counter()
for i in range(len(names)):
ht_find(lisst,names[i])
end=time.perf_counter()
finding_times.append(end-start)
#В. Удаление 50 случайных записей
for i in range(10):
names.remove("A")
rd.shuffle(names)
deletion_times=[]
for i in range(50):
start=time.perf_counter()
ht_delete(lisst,names[i])
end=time.perf_counter()
ttt=end-start
deletion_times.append(ttt)
deletion_times1.append(deletion_times)
print("Run number ",k+1)
print("Insertion time: ",insertion_times[k])
print("Finding time: ",finding_times[k])
print("Deletion times: ","\n",deletion_times)
print("\n")
temp=0
for i in range(5):
temp+=insertion_times[i]
temp=temp/5
results = [
[u"Структура", u"Режим", u"Операция", u"Время (сек)"],
["HashTable", u"отсортированный", u"вставка", insertion_times[0]],
["HashTable", u"отсортированный", u"вставка", insertion_times[1]],
["HashTable", u"отсортированный", u"вставка", insertion_times[2]],
["HashTable", u"отсортированный", u"вставка", insertion_times[3]],
["HashTable", u"сотсортированный", u"вставка", insertion_times[4]],
[u"Структура", u"Режим", u"Операция", u"Среднее время (сек)"],
["HashTable", u"отсортированный", u"вставка", temp,]
]
with codecs.open("results.csv", "a+", "utf-16") as f:
writer = csv.writer(f)
writer.writerows(results)
writer.writerows("\n")
temp=0
for i in range(5):
temp+=finding_times[i]
temp=temp/5
results = [
[u"Структура", u"Режим", u"Операция", u"Время (сек)"],
["HashTable", u"отсортированный", u"поиск", finding_times[0]],
["HashTable", u"отсортированный", u"поиск", finding_times[1]],
["HashTable", u"отсортированный", u"поиск", finding_times[2]],
["HashTable", u"отсортированный", u"поиск", finding_times[3]],
["HashTable", u"отсортированный", u"поиск", finding_times[4]],
[u"Структура", u"Режим", u"Операция", u"Среднее время (сек)"],
["HashTable", u"отсортированный", u"поиск", temp,]
]
with codecs.open("results.csv", "a+", "utf-16") as f:
writer = csv.writer(f)
writer.writerows(results)
writer.writerows("\n")
temp=0
del_times=[]
for i in range(5):
for j in range(50):
temp+=deletion_times1[i][j]
temp=temp/50
del_times.append(temp)
temp=0
temp=0
for i in range(5):
temp+=del_times[i]
temp=temp/5
results = [
[u"Структура", u"Режим", u"Операция", u"Время (сек)"],
["HashTable", u"отсортированный", u"удаление", del_times[0]],
["HashTable", u"отсортированный", u"удаление", del_times[1]],
["HashTable", u"отсортированный", u"удаление", del_times[2]],
["HashTable", u"отсортированный", u"удаление", del_times[3]],
["HashTable", u"отсортированный", u"удаление", del_times[4]],
[u"Структура", u"Режим", u"Операция", u"Среднее время (сек)"],
["HashTable", u"отсортированный", u"удаление", temp,]
]
with codecs.open("results.csv", "a+", "utf-16") as f:
writer = csv.writer(f)
writer.writerows(results)
writer.writerows("\n")
writer.writerows("\n")
records_shuffled, records_sorted = records()
run_shuffled(records_shuffled)
run_sorted(records_sorted)

View File

@ -0,0 +1,439 @@
from MP_records import records
import random as rd
import time
import csv
import codecs
def insert(lisst,name,phone):
#global lisst
if len(lisst)==0:
entry={"name":name,"phone":phone,"next":None}
lisst.append(entry)
return lisst
else:
entry={"name":name,"phone":phone,"next":None}
if lisst[0]["phone"]==phone:
lisst[0]["name"]=name
return lisst
if lisst[0]["next"]==None:
lisst[0]["next"]=entry
return lisst
else:
nexxt=lisst[0]["next"]
if nexxt["phone"]==phone:
nexxt["name"]=name
return lisst
while nexxt["next"]!=None:
nexxt=nexxt["next"]
if nexxt["phone"]==phone:
nexxt["name"]=name
return lisst
nexxt["next"]=entry
return lisst
def find(lisst, name):
#global lisst
if len(lisst)>0:
if lisst[0]["name"]==name:
return lisst[0]["phone"]
elif lisst[0]["next"]!=None:
nexxt=lisst[0]["next"]
#print(nexxt)
while nexxt["next"]!=None:
if nexxt["name"]==name:
return nexxt["phone"]
else:
nexxt=nexxt["next"]
if nexxt["name"]==name:
return nexxt["phone"]
return None
def delete(lisst,name):
#global lisst
if len(lisst)>0:
if lisst[0]["name"]==name:
if lisst[0]["next"]!=None:
lisst[0]=lisst[0]["next"]
return lisst
else:
lisst.pop()
return lisst
elif lisst[0]["next"]!=None:
if lisst[0]["next"]["name"]==name:
if lisst[0]["next"]["next"]!=None:
lisst[0]["next"]=lisst[0]["next"]["next"]
return lisst
else:
lisst[0]["next"]=None
return lisst
elif lisst[0]["next"]["next"]!=None:
nexxt1=lisst[0]["next"]
nexxt2=nexxt1["next"]
if nexxt2["name"]==name:
if nexxt2["next"]!=None:
nexxt1["next"]=nexxt2["next"]
return lisst
else:
nexxt1["next"]=None
return lisst
while nexxt2["next"]!=None:
nexxt1=nexxt2
nexxt2=nexxt1["next"]
if nexxt2["name"]==name:
if nexxt2["next"]!=None:
nexxt1["next"]=nexxt2["next"]
return lisst
else:
nexxt1["next"]=None
return lisst
def bad_sort(names,phones):
names1=[]
phones1=[]
while len(names)>0:
min_=names[0].encode()
ph=phones[0]
for i in range(len(names)):
nm=names[i].encode()
if nm<min_:
min_=nm
ph=phones[i]
#print(min_.decode()," - ",ph)
names1.append(min_.decode())
phones1.append(ph)
names.remove(min_.decode())
phones.remove(ph)
#print(names1,"\n",phones1)
return names1, phones1
def Shell(names,phones):
N = len(names)
n = N // 2
while n>0:
for i in range (0,N-n):
j=i
while j+n<N:
if (names[j].encode())>(names[j+n].encode()):
t=names[j]
t1=phones[j]
names[j]=names[j+n]
phones[j]=phones[j+n]
names[j+n]=t
phones[j+n]=t1
j=i
else:
j+=n
n=n//2
return names,phones
def list_all(lisst):
#global lisst
names=[]
phones=[]
if len(lisst)>0:
names.append(lisst[0]["name"])
phones.append(lisst[0]["phone"])
nexxt=lisst[0]["next"]
while nexxt!=None:
names.append(nexxt["name"])
phones.append(nexxt["phone"])
nexxt=nexxt["next"]
else:
print("List is empty")
return
names1, phones1 = bad_sort(names,phones)
#names1, phones1 = Shell(names,phones)
for i in range(len(names1)):
print(names1[i]," - ",phones1[i],end='')
if i%4==0:
print("\n")
else:
print(", ",end='')
print("\n")
def test():
lisst=[]
insert(lisst,"Abba",1)
insert(lisst,"Cafr",43)
insert(lisst,"Babba",2)
insert(lisst,"V",2)
insert(lisst,"Babadsaba",3)
insert(lisst,"Cabr",34)
insert(lisst,"Aaaaa",4)
insert(lisst,"Ba",5)
a=find(lisst,"Aaaaa")
print(a)
print(lisst,'\n')
delete(lisst,"Abba")
# print(lisst,'\n')
# delete("Aaaaa")
# print(lisst,'\n')
# delete("Ba")
# print(lisst,'\n')
# delete("Babadsaba")
# print(lisst,'\n')
delete(lisst,"Aaaaa")
# print(lisst,'\n')
list_all(lisst)
print(lisst,'\n')
def run_shuffled(records_shuffled):
insertion_times=[]
finding_times=[]
deletion_times1=[]
print("Shuffled list: ")
for k in range(5):
lisst=[]
rd.shuffle(records_shuffled)
#А. Вставка всех записей
start=time.perf_counter()
for i in range(len(records_shuffled)):
insert(lisst, records_shuffled[i][0], records_shuffled[i][1])
end=time.perf_counter()
insertion_times.append(end-start)
#Б. Поиск 100 случайных записей
names=[]
for i in range(100):
index=rd.randint(0,9999)
name=lisst[0]
while index>0:
name=name["next"]
index-=1
names.append(name["name"])
for i in range(10):
names.append("A")
rd.shuffle(names)
start=time.perf_counter()
for i in range(len(names)):
find(lisst,names[i])
end=time.perf_counter()
finding_times.append(end-start)
#В. Удаление 50 случайных записей
for i in range(10):
names.remove("A")
rd.shuffle(names)
deletion_times=[]
for i in range(50):
start=time.perf_counter()
delete(lisst,names[i])
end=time.perf_counter()
ttt=end-start
deletion_times.append(ttt)
deletion_times1.append(deletion_times)
print("Run number ",k+1)
print("Insertion time: ",insertion_times[k])
print("Finding time: ",finding_times[k])
print("Deletion times: ","\n",deletion_times)
print("\n")
temp=0
for i in range(5):
temp+=insertion_times[i]
temp=temp/5
results = [
[u"Структура", u"Режим", u"Операция", u"Время (сек)"],
["LinkedList", u"случайный", u"вставка", insertion_times[0]],
["LinkedList", u"случайный", u"вставка", insertion_times[1]],
["LinkedList", u"случайный", u"вставка", insertion_times[2]],
["LinkedList", u"случайный", u"вставка", insertion_times[3]],
["LinkedList", u"случайный", u"вставка", insertion_times[4]],
[u"Структура", u"Режим", u"Операция", u"Среднее время (сек)"],
["LinkedList", u"случайный", u"вставка", temp,]
]
with codecs.open("results.csv", "a+", "utf-16") as f:
writer = csv.writer(f)
writer.writerows(results)
writer.writerows("\n")
temp=0
for i in range(5):
temp+=finding_times[i]
temp=temp/5
results = [
[u"Структура", u"Режим", u"Операция", u"Время (сек)"],
["LinkedList", u"случайный", u"поиск", finding_times[0]],
["LinkedList", u"случайный", u"поиск", finding_times[1]],
["LinkedList", u"случайный", u"поиск", finding_times[2]],
["LinkedList", u"случайный", u"поиск", finding_times[3]],
["LinkedList", u"случайный", u"поиск", finding_times[4]],
[u"Структура", u"Режим", u"Операция", u"Среднее время (сек)"],
["LinkedList", u"случайный", u"поиск", temp,]
]
with codecs.open("results.csv", "a+", "utf-16") as f:
writer = csv.writer(f)
writer.writerows(results)
writer.writerows("\n")
temp=0
del_times=[]
for i in range(5):
for j in range(50):
temp+=deletion_times1[i][j]
temp=temp/50
del_times.append(temp)
temp=0
temp=0
for i in range(5):
temp+=del_times[i]
temp=temp/5
results = [
[u"Структура", u"Режим", u"Операция", u"Время (сек)"],
["LinkedList", u"случайный", u"удаление", del_times[0]],
["LinkedList", u"случайный", u"удаление", del_times[1]],
["LinkedList", u"случайный", u"удаление", del_times[2]],
["LinkedList", u"случайный", u"удаление", del_times[3]],
["LinkedList", u"случайный", u"удаление", del_times[4]],
[u"Структура", u"Режим", u"Операция", u"Среднее время (сек)"],
["LinkedList", u"случайный", u"удаление", temp,]
]
with codecs.open("results.csv", "a+", "utf-16") as f:
writer = csv.writer(f)
writer.writerows(results)
writer.writerows("\n")
writer.writerows("\n")
def run_sorted(records_shuffled):
insertion_times=[]
finding_times=[]
deletion_times1=[]
print("Sorted list: ")
for k in range(5):
lisst=[]
#А. Вставка всех записей
start=time.perf_counter()
for i in range(len(records_shuffled)):
insert(lisst, records_shuffled[i][0], records_shuffled[i][1])
end=time.perf_counter()
insertion_times.append(end-start)
#Б. Поиск 100 случайных записей
names=[]
for i in range(100):
index=rd.randint(0,9999)
name=lisst[0]
while index>0:
name=name["next"]
index-=1
names.append(name["name"])
for i in range(10):
names.append("A")
rd.shuffle(names)
start=time.perf_counter()
for i in range(len(names)):
find(lisst,names[i])
end=time.perf_counter()
finding_times.append(end-start)
#В. Удаление 50 случайных записей
for i in range(10):
names.remove("A")
rd.shuffle(names)
deletion_times=[]
for i in range(50):
start=time.perf_counter()
delete(lisst,names[i])
end=time.perf_counter()
ttt=end-start
deletion_times.append(ttt)
deletion_times1.append(deletion_times)
print("Run number ",k+1)
print("Insertion time: ",insertion_times[k])
print("Finding time: ",finding_times[k])
print("Deletion times: ","\n",deletion_times)
print("\n")
temp=0
for i in range(5):
temp+=insertion_times[i]
temp=temp/5
results = [
[u"Структура", u"Режим", u"Операция", u"Время (сек)"],
["LinkedList", u"отсортированный", u"вставка", insertion_times[0]],
["LinkedList", u"отсортированный", u"вставка", insertion_times[1]],
["LinkedList", u"отсортированный", u"вставка", insertion_times[2]],
["LinkedList", u"отсортированный", u"вставка", insertion_times[3]],
["LinkedList", u"сотсортированный", u"вставка", insertion_times[4]],
[u"Структура", u"Режим", u"Операция", u"Среднее время (сек)"],
["LinkedList", u"отсортированный", u"вставка", temp,]
]
with codecs.open("results.csv", "a+", "utf-16") as f:
writer = csv.writer(f)
writer.writerows(results)
writer.writerows("\n")
temp=0
for i in range(5):
temp+=finding_times[i]
temp=temp/5
results = [
[u"Структура", u"Режим", u"Операция", u"Время (сек)"],
["LinkedList", u"отсортированный", u"поиск", finding_times[0]],
["LinkedList", u"отсортированный", u"поиск", finding_times[1]],
["LinkedList", u"отсортированный", u"поиск", finding_times[2]],
["LinkedList", u"отсортированный", u"поиск", finding_times[3]],
["LinkedList", u"отсортированный", u"поиск", finding_times[4]],
[u"Структура", u"Режим", u"Операция", u"Среднее время (сек)"],
["LinkedList", u"отсортированный", u"поиск", temp,]
]
with codecs.open("results.csv", "a+", "utf-16") as f:
writer = csv.writer(f)
writer.writerows(results)
writer.writerows("\n")
temp=0
del_times=[]
for i in range(5):
for j in range(50):
temp+=deletion_times1[i][j]
temp=temp/50
del_times.append(temp)
temp=0
temp=0
for i in range(5):
temp+=del_times[i]
temp=temp/5
results = [
[u"Структура", u"Режим", u"Операция", u"Время (сек)"],
["LinkedList", u"отсортированный", u"удаление", del_times[0]],
["LinkedList", u"отсортированный", u"удаление", del_times[1]],
["LinkedList", u"отсортированный", u"удаление", del_times[2]],
["LinkedList", u"отсортированный", u"удаление", del_times[3]],
["LinkedList", u"отсортированный", u"удаление", del_times[4]],
[u"Структура", u"Режим", u"Операция", u"Среднее время (сек)"],
["LinkedList", u"отсортированный", u"удаление", temp,]
]
with codecs.open("results.csv", "a+", "utf-16") as f:
writer = csv.writer(f)
writer.writerows(results)
writer.writerows("\n")
writer.writerows("\n")
records_shuffled, records_sorted = records()
run_shuffled(records_shuffled)
run_sorted(records_sorted)

28
osininyai/[1]MP_names.py Normal file
View File

@ -0,0 +1,28 @@
import random as rd
import string
up=list(string.ascii_uppercase)
consonants=["b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z"]
vowels=["a", "e", "i", "o", "u", "y"]
count=0
names=[]
while count<5000:
name=""
temp=rd.randint(0, len(up)-1)
name+=up[temp]
length=rd.randint(2,9)
for i in range(length):
temp=rd.randint(0, 1)
if temp==0:
letter=rd.randint(0, len(vowels)-1)
name+=vowels[letter]
else:
letter=rd.randint(0, len(consonants)-1)
name+=consonants[letter]
names.append(name)
count+=1
f=open("names.txt","w")
for i in names:
f.write(i)
f.write("\n")
f.close()

View File

@ -0,0 +1,73 @@
import random as rd
def Shell(arr):
N = len(arr)
n = N // 2
while n>0:
for i in range (0,N-n):
j=i
while j+n<N:
if arr[j]>arr[j+n]:
t=arr[j]
arr[j]=arr[j+n]
arr[j+n]=t
j=i
else:
j+=n
n=n//2
return arr
def records():
phones=[]
first=0
second=0
third=0
fourth=0
for i in range(10000):
phones.append(str(first)+str(second)+str(third)+str(fourth))
fourth+=1
if fourth==10:
third+=1
fourth=0
if third==10:
second+=1
third=0
if second==10:
first+=1
second=0
phones2=phones.copy()
f=open("names.txt","r")
count=0
names=[]
while count<5000:
name=f.readline()
names.append(name[:len(name)-1])
names.append(name[:len(name)-1])
count+=1
f.close()
names_sorted=names.copy()
for i in range(10000):
names_sorted[i]=names_sorted[i].encode()
Shell(names_sorted)
for i in range(10000):
names_sorted[i]=names_sorted[i].decode()
records_shuffled=[]
records_sorted=[]
count=0
while count<10000:
name_var=rd.randint(0,len(names)-1)
phone_var=rd.randint(0,len(phones2)-1)
records_shuffled.append((names[name_var],phones[count]))
records_sorted.append((names_sorted[count],phones2[phone_var]))
names.remove(names[name_var])
phones2.remove(phones2[phone_var])
count+=1
rd.shuffle(records_shuffled)
return records_shuffled, records_sorted
#print(records_shuffled)
#print(records_sorted)

5000
osininyai/[1]names.txt Normal file

File diff suppressed because it is too large Load Diff

Binary file not shown.

Binary file not shown.

Binary file not shown.
Can't render this file because it has a wrong number of fields in line 9.