[1]task1 #321

Merged
IvanBoy merged 8 commits from kuzminskiyaa/2026-rff_mp:task1 into develop 2026-05-30 11:57:48 +00:00
5 changed files with 269 additions and 0 deletions

View File

@ -0,0 +1,250 @@
import time
import random
import csv
import matplotlib.pyplot as plt
import numpy as np
def ll_insert(head, name, phone):
new_node = {'name': name, 'phone': phone, 'next': None}
if head is None:
return new_node
current = head
while current:
if current['name'] == name:
current['phone'] = phone
return head
if current['next'] is None:
current['next'] = new_node
return head
current = current['next']
return head
def ll_find(head, name):
current = head
while current:
if current['name'] == name:
return current['phone']
current = current['next']
return None
def ll_delete(head, name):
if head is None:
return None
if head['name'] == name:
return head['next']
current = head
while current['next']:
if current['next']['name'] == name:
current['next'] = current['next']['next']
return head
current = current['next']
return head
def ll_list_all(head):
records = []
current = head
while current:
records.append((current['name'], current['phone']))
current = current['next']
return sorted(records, key=lambda x: x[0])
def hash_func(name, size):
return sum(ord(c) for c in name) % size
def ht_create(size=1000):
return [None] * size
def ht_insert(table, name, phone):
idx = hash_func(name, len(table))
table[idx] = ll_insert(table[idx], name, phone)
def ht_find(table, name):
idx = hash_func(name, len(table))
return ll_find(table[idx], name)
def ht_delete(table, name):
idx = hash_func(name, len(table))
table[idx] = ll_delete(table[idx], name)
def ht_list_all(table):
records = []
for bucket in table:
current = bucket
while current:
records.append((current['name'], current['phone']))
current = current['next']
return sorted(records, key=lambda x: x[0])
def bst_insert(root, name, phone):
if root is None:
return {'name': name, 'phone': phone, 'left': None, 'right': None}
if name < root['name']:
root['left'] = bst_insert(root['left'], name, phone)
elif name > root['name']:
root['right'] = bst_insert(root['right'], name, phone)
else:
root['phone'] = phone
return root
def bst_find(root, name):
if root is None:
return None
if name == root['name']:
return root['phone']
elif name < root['name']:
return bst_find(root['left'], name)
else:
return bst_find(root['right'], name)
def bst_min(node):
while node and node['left']:
node = node['left']
return node
def bst_delete(root, name):
if root is None:
return None
if name < root['name']:
root['left'] = bst_delete(root['left'], name)
elif name > root['name']:
root['right'] = bst_delete(root['right'], name)
else:
if root['left'] is None:
return root['right']
if root['right'] is None:
return root['left']
min_node = bst_min(root['right'])
root['name'] = min_node['name']
root['phone'] = min_node['phone']
root['right'] = bst_delete(root['right'], min_node['name'])
return root
def bst_list_all(root):
records = []
if root:
records.extend(bst_list_all(root['left']))
records.append((root['name'], root['phone']))
records.extend(bst_list_all(root['right']))
return records
def generate_data(n=2000):
random_data = [(f"User_{i:05d}", str(i)) for i in range(n)]
random.shuffle(random_data)
sorted_data = sorted(random_data, key=lambda x: x[0])
return random_data, sorted_data
def run_test(data, struct_name, create, insert, find, delete):
times = {'insert': [], 'search': [], 'delete': []}
for _ in range(5):
s = create()
start = time.perf_counter()
if struct_name == 'LinkedList' or struct_name == 'BST':
for name, phone in data:
s = insert(s, name, phone)
else:
for name, phone in data:
insert(s, name, phone)
times['insert'].append(time.perf_counter() - start)
names = [random.choice(data)[0] for _ in range(100)] + [f"None_{i}" for i in range(10)]
start = time.perf_counter()
for name in names:
find(s, name)
times['search'].append(time.perf_counter() - start)
del_names = [random.choice(data)[0] for _ in range(50)]
start = time.perf_counter()
for name in del_names:
if struct_name == 'LinkedList' or struct_name == 'BST':
s = delete(s, name)
else:
delete(s, name)
times['delete'].append(time.perf_counter() - start)
return {op: sum(t)/len(t) for op, t in times.items()}
def plot_results(data_matrix):
structures = ['LinkedList', 'HashTable', 'BST']
operations = ['insert', 'search', 'delete']
modes = ['random', 'sorted']
fig, axes = plt.subplots(2, 2, figsize=(14, 12))
x = np.arange(len(structures))
width = 0.25
for i, op in enumerate(operations):
values = [data_matrix[s]['random'][op] for s in structures]
axes[0,0].bar(x + i*width, values, width, label=op)
axes[0,0].set_xlabel('Структура данных')
axes[0,0].set_ylabel('Время (секунды)')
axes[0,0].set_title('Случайный порядок данных')
axes[0,0].set_xticks(x + width, structures)
axes[0,0].legend()
axes[0,0].grid(True, alpha=0.3)
for i, op in enumerate(operations):
values = [data_matrix[s]['sorted'][op] for s in structures]
axes[0,1].bar(x + i*width, values, width, label=op)
axes[0,1].set_xlabel('Структура данных')
axes[0,1].set_ylabel('Время (секунды)')
axes[0,1].set_title('Отсортированный порядок данных')
axes[0,1].set_xticks(x + width, structures)
axes[0,1].legend()
axes[0,1].grid(True, alpha=0.3)
x = np.arange(len(operations))
width = 0.35
for i, mode in enumerate(modes):
values = [data_matrix['BST'][mode][op] for op in operations]
axes[1,0].bar(x + i*width, values, width, label=mode)
axes[1,0].set_xlabel('Операция')
axes[1,0].set_ylabel('Время (секунды)')
axes[1,0].set_title('BST: влияние порядка данных')
axes[1,0].set_xticks(x + width/2, operations)
axes[1,0].legend()
axes[1,0].grid(True, alpha=0.3)
for struct in structures:
times_random = [data_matrix[struct]['random'][op] for op in operations]
times_sorted = [data_matrix[struct]['sorted'][op] for op in operations]
axes[1,1].plot(operations, times_random, marker='o', label=f'{struct} случайный')
axes[1,1].plot(operations, times_sorted, marker='s', linestyle='--', label=f'{struct} отсортированный')
axes[1,1].set_yscale('log')
axes[1,1].set_xlabel('Операция')
axes[1,1].set_ylabel('Время (секунды) - логарифмическая шкала')
axes[1,1].set_title('Сравнение производительности')
axes[1,1].legend()
axes[1,1].grid(True, alpha=0.3)
plt.tight_layout()
plt.savefig('performance_graphs.png')
plt.show()
random_data, sorted_data = generate_data(2000)
structs = [
('LinkedList', lambda: None, ll_insert, ll_find, ll_delete),
('HashTable', lambda: ht_create(2000), ht_insert, ht_find, ht_delete),
('BST', lambda: None, bst_insert, bst_find, bst_delete)
]
data_matrix = {'LinkedList': {'random': {}, 'sorted': {}},
'HashTable': {'random': {}, 'sorted': {}},
'BST': {'random': {}, 'sorted': {}}}
for name, create, insert, find, delete in structs:
for order, data in [('random', random_data), ('sorted', sorted_data)]:
times = run_test(data, name, create, insert, find, delete)
data_matrix[name][order] = times
with open('results.csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(['структура', 'порядок_данных', 'операция', 'время_секунды'])
for name in ['LinkedList', 'HashTable', 'BST']:
for order in ['random', 'sorted']:
for op in ['insert', 'search', 'delete']:
writer.writerow([name, order, op, data_matrix[name][order][op]])
plot_results(data_matrix)
print("\n РЕЗУЛЬТАТЫ")
for name in ['LinkedList', 'HashTable', 'BST']:
for order in ['random', 'sorted']:
print(f"{name} {order}: вставка={data_matrix[name][order]['insert']:.6f}, поиск={data_matrix[name][order]['search']:.6f}, удаление={data_matrix[name][order]['delete']:.6f}")

Binary file not shown.

After

Width:  |  Height:  |  Size: 185 KiB

View File

@ -0,0 +1,19 @@
структура,порядок_данных,операция,время_секунды
LinkedList,random,insert,0.4430991000015638
LinkedList,random,search,0.019253799998841713
LinkedList,random,delete,0.012056579999625682
LinkedList,sorted,insert,0.4242827600013698
LinkedList,sorted,search,0.019056700001237915
LinkedList,sorted,delete,0.010539219998463523
HashTable,random,insert,0.025478639999346343
HashTable,random,search,0.0009841200007940643
HashTable,random,delete,0.0006616600003326312
HashTable,sorted,insert,0.025232040000264532
HashTable,sorted,search,0.0010681599989766255
HashTable,sorted,delete,0.0006402399987564423
BST,random,insert,0.004588020002120175
BST,random,search,0.00022194000048330053
BST,random,delete,0.00013212000048952178
BST,sorted,insert,0.660595199999807
BST,sorted,search,0.025651479998487048
BST,sorted,delete,0.015685519999533427
1 структура порядок_данных операция время_секунды
2 LinkedList random insert 0.4430991000015638
3 LinkedList random search 0.019253799998841713
4 LinkedList random delete 0.012056579999625682
5 LinkedList sorted insert 0.4242827600013698
6 LinkedList sorted search 0.019056700001237915
7 LinkedList sorted delete 0.010539219998463523
8 HashTable random insert 0.025478639999346343
9 HashTable random search 0.0009841200007940643
10 HashTable random delete 0.0006616600003326312
11 HashTable sorted insert 0.025232040000264532
12 HashTable sorted search 0.0010681599989766255
13 HashTable sorted delete 0.0006402399987564423
14 BST random insert 0.004588020002120175
15 BST random search 0.00022194000048330053
16 BST random delete 0.00013212000048952178
17 BST sorted insert 0.660595199999807
18 BST sorted search 0.025651479998487048
19 BST sorted delete 0.015685519999533427

Binary file not shown.

Binary file not shown.