forked from UNN/2026-rff_mp
[12] adding time for LinkedList
This commit is contained in:
parent
f9461cd010
commit
a9d5835d7f
|
|
@ -1,4 +1,5 @@
|
|||
import random as rnd
|
||||
import time
|
||||
#############################################################################################
|
||||
|
||||
def sort_list(name_list):
|
||||
|
|
@ -13,8 +14,10 @@ def hash_key(name):
|
|||
h_key = sum(ord(ch) for ch in name)
|
||||
return h_key
|
||||
|
||||
|
||||
|
||||
def create_name_phone(i):
|
||||
name = f"User_{i:03d}"
|
||||
phone = f"{rnd.randint(100, 999)}-{rnd.randint(100, 999)}"
|
||||
return (name, phone)
|
||||
###########################################################################################################################
|
||||
|
||||
def ll_insert(head, name, phone):
|
||||
|
|
@ -74,42 +77,40 @@ def ll_list_all(head):
|
|||
################################################################################################################################
|
||||
|
||||
|
||||
def LinkedList(head):
|
||||
def LinkedList(head, phone_book):
|
||||
|
||||
print('======== TESTING LL_INSERT ==========')
|
||||
Name = ['Dima', 'Ivan', 'Maxim', 'Alex', 'Olga', 'Lena']
|
||||
start_insert = time.perf_counter()
|
||||
for i in range(len(phone_book)):
|
||||
|
||||
for _ in range(10):
|
||||
name = Name[rnd.randint(0, len(Name) - 1)]
|
||||
phone = str(rnd.randint(0,9)) + str(rnd.randint(0,9)) + str(rnd.randint(0,9)) + '-' + \
|
||||
str(rnd.randint(0,9)) + str(rnd.randint(0,9)) + str(rnd.randint(0,9))
|
||||
print(name, phone)
|
||||
head = ll_insert(head, name, phone)
|
||||
print(head)
|
||||
print('-----------------------------------\n')
|
||||
print('======== END TESTING ================\n\n')
|
||||
head = ll_insert(head, phone_book[i][0], phone_book[i][1])
|
||||
#print(head)
|
||||
end_insert = time.perf_counter()
|
||||
time_insert = end_insert - start_insert
|
||||
|
||||
|
||||
print('======== TESTING LL_FIND ==========')
|
||||
Name.append('Masha')
|
||||
for i in range(len(Name)):
|
||||
name = Name[i]
|
||||
print(name, ":", ll_find(head, name))
|
||||
print("======== END TESTING =============\n\n")
|
||||
|
||||
print("============ TESTING LL_LIST_ALL AND SORT_LIST =============")
|
||||
print(*sort_list(ll_list_all(head)))
|
||||
print("============ END TESTING ===============\n\n")
|
||||
|
||||
print('========== TESTING LL_DELETE ==========')
|
||||
start_find = time.perf_counter()
|
||||
for _ in range(100):
|
||||
name = create_name_phone(rnd.randint(0, 999))[0]
|
||||
#print(name, ":", ll_find(head, name))
|
||||
end_find = time.perf_counter()
|
||||
time_find = end_find - start_find
|
||||
|
||||
for _ in range(2):
|
||||
name = Name[rnd.randint(0, 4)]
|
||||
|
||||
start_delete = time.perf_counter()
|
||||
for i in range(110):
|
||||
if i <= 99: name = f"User_{rnd.randint(0,999):03d}"
|
||||
else: name = f"None_{i:03d}"
|
||||
head = ll_delete(head, name)
|
||||
|
||||
print(head)
|
||||
print(name, ":", ll_find(head, name))
|
||||
print("========= END TESTING ==============\n\n")
|
||||
end_delete = time.perf_counter()
|
||||
time_delete = end_delete - start_delete
|
||||
|
||||
|
||||
start_list = time.perf_counter()
|
||||
name_list = sort_list(ll_list_all(head))
|
||||
#print(*name_list)
|
||||
end_list = time.perf_counter()
|
||||
time_list = end_list - start_list
|
||||
|
||||
return (time_insert, time_find, time_delete, time_list)
|
||||
|
||||
#########################################################################################################
|
||||
|
||||
|
|
@ -248,8 +249,9 @@ def bst_list_all(root, name_list = []):
|
|||
def bst_delete(root, name):
|
||||
|
||||
running = root
|
||||
old_running = running
|
||||
|
||||
while running is not None:
|
||||
while old_running is not None:
|
||||
node = hash_key(running['name'])
|
||||
sheet = hash_key(name)
|
||||
if name == running['name']:
|
||||
|
|
@ -265,6 +267,10 @@ def bst_delete(root, name):
|
|||
running = running['left']
|
||||
|
||||
step = ['left', 'right']
|
||||
|
||||
if running['left'] is None and running['right'] is None:
|
||||
old_running = None
|
||||
return root
|
||||
|
||||
if running['left'] is None:
|
||||
old_running[step[flag]] = running['right']
|
||||
|
|
@ -335,9 +341,38 @@ def BinarySearchTree(root):
|
|||
################################################################################################
|
||||
def main():
|
||||
|
||||
LinkedList(None)
|
||||
HashTable([[] for _ in range(10)])
|
||||
BinarySearchTree(None)
|
||||
phone_book = []
|
||||
for i in range(1000):
|
||||
phone_book.append(create_name_phone(i))
|
||||
for _ in range(9000):
|
||||
phone_book.append(create_name_phone(rnd.randint(0, 999)))
|
||||
|
||||
phone_book_not_sorted = phone_book.copy()
|
||||
rnd.shuffle(phone_book_not_sorted)
|
||||
|
||||
phone_book_sorted = phone_book.copy()
|
||||
phone_book_sorted = sort_list(phone_book_sorted)
|
||||
replay = 10
|
||||
|
||||
|
||||
Time_ll_not_sorted = {'insert': 0, 'find': 0, 'delete': 0, 'list': 0}
|
||||
Time_ll_sorted = {'insert': 0, 'find': 0, 'delete': 0, 'list': 0}
|
||||
|
||||
for _ in range(replay):
|
||||
time_ll_not_sorted = LinkedList(None, phone_book_not_sorted)
|
||||
for i, key in enumerate(Time_ll_not_sorted):
|
||||
Time_ll_not_sorted[key] += time_ll_not_sorted[i]/replay
|
||||
print(Time_ll_not_sorted)
|
||||
|
||||
for _ in range(replay):
|
||||
time_ll_sorted = LinkedList(None, phone_book_sorted)
|
||||
for i, key in enumerate(Time_ll_sorted):
|
||||
Time_ll_sorted[key] += time_ll_sorted[i]/replay
|
||||
print(Time_ll_sorted)
|
||||
|
||||
|
||||
#HashTable([[] for _ in range(10)])
|
||||
#BinarySearchTree(None)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user