import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

df = pd.read_csv('results.csv')
mean_df = df.groupby(['Structure','Mode','Operation'])['Time_sec'].mean().reset_index()

fig, axes = plt.subplots(1, 3, figsize=(14,5))
operations = ['insert','find','delete']
titles = ['Insertion', 'Search', 'Deletion']

for ax, op, title in zip(axes, operations, titles):
    subset = mean_df[mean_df['Operation'] == op]
    structures = subset['Structure'].unique()
    x = np.arange(len(structures))
    width = 0.35
    random_vals = []
    sorted_vals = []
    for s in structures:
        r = subset[(subset['Structure']==s) & (subset['Mode']=='random')]['Time_sec'].values
        s_vals = subset[(subset['Structure']==s) & (subset['Mode']=='sorted')]['Time_sec'].values
        random_vals.append(r[0] if len(r) else 0)
        sorted_vals.append(s_vals[0] if len(s_vals) else 0)
    ax.bar(x - width/2, random_vals, width, label='random')
    ax.bar(x + width/2, sorted_vals, width, label='sorted')
    ax.set_xticks(x)
    ax.set_xticklabels(structures)
    ax.set_ylabel('Time (seconds)')
    ax.set_title(title)
    ax.legend()

plt.tight_layout()
plt.savefig('performance.png', dpi=150)
plt.show()
print("График сохранён (performance.png)")