Основная функция main()
This commit is contained in:
parent
a8baea68c7
commit
b652b1515c
|
|
@ -407,5 +407,98 @@ def plot_attempts_graphs(data, op_name, op_title):
|
||||||
plt.savefig(f'{op_name}_5attempts.png', dpi=150)
|
plt.savefig(f'{op_name}_5attempts.png', dpi=150)
|
||||||
plt.show()
|
plt.show()
|
||||||
print(f" График {op_name}_5attempts.png сохранён")
|
print(f" График {op_name}_5attempts.png сохранён")
|
||||||
|
|
||||||
|
def main():
|
||||||
|
print("ЛАБОРАТОРНАЯ РАБОТА №1: СРАВНЕНИЕ СТРУКТУР ДАННЫХ")
|
||||||
|
|
||||||
|
print("\n1. Генерация тестовых данных...")
|
||||||
|
records = generate_records(N)
|
||||||
|
random.shuffle(records)
|
||||||
|
records_sorted = sorted(records, key=lambda x: x[0])
|
||||||
|
print(f" Сгенерировано {N} записей")
|
||||||
|
|
||||||
|
results = []
|
||||||
|
struct_names = {'ll': 'Связный список', 'ht': 'Хеш-таблица', 'bst': 'Двоичное дерево'}
|
||||||
|
mode_names = {'shuffled': 'случайный', 'sorted': 'отсортированный'}
|
||||||
|
op_names = {'insert': 'Вставка всех записей', 'find': 'Поиск записей', 'delete': 'Удаление записей'}
|
||||||
|
|
||||||
|
insert_data = {'ll': {}, 'ht': {}, 'bst': {}}
|
||||||
|
search_data = {'ll': {}, 'ht': {}, 'bst': {}}
|
||||||
|
delete_data = {'ll': {}, 'ht': {}, 'bst': {}}
|
||||||
|
|
||||||
|
# Вставка
|
||||||
|
print("\n2. Тестирование ВСТАВКИ (10000 записей):")
|
||||||
|
for struct in ['ll', 'ht', 'bst']:
|
||||||
|
print(f"\n {struct_names[struct]}:")
|
||||||
|
times_sh = measure_insertion(struct, records)
|
||||||
|
times_so = measure_insertion(struct, records_sorted)
|
||||||
|
insert_data[struct]['shuffled'] = times_sh
|
||||||
|
insert_data[struct]['sorted'] = times_so
|
||||||
|
print(f" случайный: {[round(t,6) for t in times_sh]}, среднее = {sum(times_sh)/len(times_sh):.6f}")
|
||||||
|
print(f" отсортированный: {[round(t,6) for t in times_so]}, среднее = {sum(times_so)/len(times_so):.6f}")
|
||||||
|
results.append([struct_names[struct], mode_names['shuffled'], op_names['insert'], sum(times_sh)/len(times_sh)] + times_sh)
|
||||||
|
results.append([struct_names[struct], mode_names['sorted'], op_names['insert'], sum(times_so)/len(times_so)] + times_so)
|
||||||
|
|
||||||
|
# Поиск
|
||||||
|
print("\n3. Тестирование ПОИСКА (110 запросов):")
|
||||||
|
for struct in ['ll', 'ht', 'bst']:
|
||||||
|
print(f"\n {struct_names[struct]}:")
|
||||||
|
structure_sh = build_structure(struct, records)
|
||||||
|
times_find_sh = measure_search(struct, structure_sh, records)
|
||||||
|
search_data[struct]['shuffled'] = times_find_sh
|
||||||
|
print(f" случайный: {[round(t,6) for t in times_find_sh]}, среднее = {sum(times_find_sh)/len(times_find_sh):.6f}")
|
||||||
|
results.append([struct_names[struct], mode_names['shuffled'], op_names['find'], sum(times_find_sh)/len(times_find_sh)] + times_find_sh)
|
||||||
|
|
||||||
|
structure_so = build_structure(struct, records_sorted)
|
||||||
|
times_find_so = measure_search(struct, structure_so, records_sorted)
|
||||||
|
search_data[struct]['sorted'] = times_find_so
|
||||||
|
print(f" отсортированный: {[round(t,6) for t in times_find_so]}, среднее = {sum(times_find_so)/len(times_find_so):.6f}")
|
||||||
|
results.append([struct_names[struct], mode_names['sorted'], op_names['find'], sum(times_find_so)/len(times_find_so)] + times_find_so)
|
||||||
|
|
||||||
|
# Удаление
|
||||||
|
print("\n4. Тестирование УДАЛЕНИЯ (50 записей):")
|
||||||
|
for struct in ['ll', 'ht', 'bst']:
|
||||||
|
print(f"\n {struct_names[struct]}:")
|
||||||
|
times_del_sh = measure_deletion(struct, records)
|
||||||
|
delete_data[struct]['shuffled'] = times_del_sh
|
||||||
|
print(f" случайный: {[round(t,6) for t in times_del_sh]}, среднее = {sum(times_del_sh)/len(times_del_sh):.6f}")
|
||||||
|
results.append([struct_names[struct], mode_names['shuffled'], op_names['delete'], sum(times_del_sh)/len(times_del_sh)] + times_del_sh)
|
||||||
|
|
||||||
|
times_del_so = measure_deletion(struct, records_sorted)
|
||||||
|
delete_data[struct]['sorted'] = times_del_so
|
||||||
|
print(f" отсортированный: {[round(t,6) for t in times_del_so]}, среднее = {sum(times_del_so)/len(times_del_so):.6f}")
|
||||||
|
results.append([struct_names[struct], mode_names['sorted'], op_names['delete'], sum(times_del_so)/len(times_del_so)] + times_del_so)
|
||||||
|
|
||||||
|
# Сохранение CSV
|
||||||
|
print("\n5. Сохранение результатов в CSV...")
|
||||||
|
with open("phonebook_results.csv", "w", newline="", encoding="utf-8") as f:
|
||||||
|
writer = csv.writer(f)
|
||||||
|
writer.writerow(['Структура', 'Режим', 'Операция', 'Среднее', 'Замер1', 'Замер2', 'Замер3', 'Замер4', 'Замер5'])
|
||||||
|
writer.writerows(results)
|
||||||
|
print(" CSV-файл сохранён: phonebook_results.csv")
|
||||||
|
|
||||||
|
# Сводная таблица
|
||||||
|
print("СВОДНАЯ ТАБЛИЦА РЕЗУЛЬТАТОВ (средние значения)")
|
||||||
|
print(f"{'Структура':<20} {'Режим':<15} {'Вставка(с)':<12} {'Поиск(с)':<12} {'Удаление(с)':<12}")
|
||||||
|
print("-" * 70)
|
||||||
|
|
||||||
|
for struct in ['ll', 'ht', 'bst']:
|
||||||
|
for mode in ['shuffled', 'sorted']:
|
||||||
|
ins_avg = sum(insert_data[struct][mode]) / REPEATS
|
||||||
|
sea_avg = sum(search_data[struct][mode]) / REPEATS
|
||||||
|
del_avg = sum(delete_data[struct][mode]) / REPEATS
|
||||||
|
mode_rus = "случайный" if mode == 'shuffled' else "отсортированный"
|
||||||
|
print(f"{struct_names[struct]:<20} {mode_rus:<15} {ins_avg:<12.6f} {sea_avg:<12.6f} {del_avg:<12.6f}")
|
||||||
|
|
||||||
|
# Построение графиков
|
||||||
|
print("\n6. Построение графиков...")
|
||||||
|
try:
|
||||||
|
plot_bar_charts(insert_data, search_data, delete_data)
|
||||||
|
plot_attempts_graphs(insert_data, 'insert', 'Вставка')
|
||||||
|
plot_attempts_graphs(search_data, 'search', 'Поиск')
|
||||||
|
plot_attempts_graphs(delete_data, 'delete', 'Удаление')
|
||||||
|
print("\n Все графики успешно сохранены!")
|
||||||
|
except Exception as e:
|
||||||
|
print(f" Ошибка при построении графиков: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user