Files
algo2025/sort_closet/code-sorting/plot_results.py
2025-12-18 16:00:22 +08:00

139 lines
9.7 KiB
Python

import matplotlib.pyplot as plt
import pandas as pd
import io
output = """
--- Running Performance Analysis ---
Algorithm Size Avg Time (s) Avg Comparisons Avg Moves Correct?
--------------------------------------------------------------------------------------------------------------
InsertSort 100 0.000006 2628 2633 Yes
InsertSort 1000 0.000108 251895 251902 Yes
InsertSort 10000 0.010841 25002574 25002583 Yes
InsertSort 50000 -> Skipped
InsertSort 100000 -> Skipped
InsertSort 500000 -> Skipped
BubbleSort 100 0.000018 4891 7118 Yes
BubbleSort 1000 0.001527 498086 754888 Yes
BubbleSort 10000 0.147654 49984024 74752463 Yes
BubbleSort 50000 -> Skipped
BubbleSort 100000 -> Skipped
BubbleSort 500000 -> Skipped
ShellSort 100 0.000003 859 911 Yes
ShellSort 1000 0.000040 15404 15912 Yes
ShellSort 10000 0.000577 263303 268383 Yes
ShellSort 50000 0.003500 1883075 1908568 Yes
ShellSort 100000 0.007738 4329053 4379481 Yes
ShellSort 500000 0.047314 28756169 29008608 Yes
MergeSort 100 0.000005 541 1344 Yes
MergeSort 1000 0.000053 8710 19952 Yes
MergeSort 10000 0.000639 120467 267232 Yes
MergeSort 50000 0.003562 718133 1568928 Yes
MergeSort 100000 0.007466 1536366 3337856 Yes
MergeSort 500000 0.042618 8837077 18951424 Yes
QuickSort 100 0.000002 633 1146 Yes
QuickSort 1000 0.000027 11070 18485 Yes
QuickSort 10000 0.000340 156205 257969 Yes
QuickSort 50000 0.001971 938346 1488604 Yes
QuickSort 100000 0.004147 2013584 3293229 Yes
QuickSort 500000 0.023807 11835910 18761405 Yes
QuickSortOpt 100 0.000002 723 1306 Yes
QuickSortOpt 1000 0.000029 10853 19089 Yes
QuickSortOpt 10000 0.000359 152089 263599 Yes
QuickSortOpt 50000 0.002079 889846 1472662 Yes
QuickSortOpt 100000 0.004246 1894396 3081447 Yes
QuickSortOpt 500000 0.025016 10915240 17611893 Yes
QuickSort3Way 100 0.000003 756 693 Yes
QuickSort3Way 1000 0.000039 12003 9189 Yes
QuickSort3Way 10000 0.000479 168509 114458 Yes
QuickSort3Way 50000 0.002715 1013555 652409 Yes
QuickSort3Way 100000 0.005945 2123298 1376911 Yes
QuickSort3Way 500000 0.032901 12324462 7696985 Yes
DualPivotSort 100 0.000002 624 884 Yes
DualPivotSort 1000 0.000026 10635 12297 Yes
DualPivotSort 10000 0.000331 151783 170443 Yes
DualPivotSort 50000 0.001911 908308 974849 Yes
DualPivotSort 100000 0.004047 1926590 2053087 Yes
DualPivotSort 500000 0.022747 11189537 11712207 Yes
"""
# Use StringIO to treat the string data as a file
data = io.StringIO(output)
# Read the data, skipping the header and footer
df = pd.read_csv(data, sep='\s+', skiprows=3, skipfooter=1, engine='python',
names=['Algorithm', 'Size', 'Avg Time (s)', 'Avg Comparisons', 'Avg Moves', 'Correct?'])
# Drop the 'Correct?' column as it's not needed for plotting
df = df.drop(columns=['Correct?'])
# Remove rows with 'Skipped' values
df = df[~df.isin(['->', 'Skipped']).any(axis=1)]
# Convert columns to numeric types
for col in ['Size', 'Avg Time (s)', 'Avg Comparisons', 'Avg Moves']:
df[col] = pd.to_numeric(df[col])
# Get the list of algorithms
algorithms = df['Algorithm'].unique()
# Plotting
plt.style.use('ggplot')
# --- Plot 1: Average Time vs. Size ---
plt.figure(figsize=(12, 8))
for algo in algorithms:
subset = df[df['Algorithm'] == algo]
plt.plot(subset['Size'], subset['Avg Time (s)'], marker='o', linestyle='-', label=algo)
plt.title('Average Time vs. Input Size')
plt.xlabel('Input Size (n)')
plt.ylabel('Average Time (seconds)')
plt.xscale('log')
plt.yscale('log')
plt.legend()
plt.grid(True, which="both", ls="--")
plt.savefig('average_time_vs_size.png')
plt.close()
# --- Plot 2: Average Comparisons vs. Size ---
plt.figure(figsize=(12, 8))
for algo in algorithms:
subset = df[df['Algorithm'] == algo]
plt.plot(subset['Size'], subset['Avg Comparisons'], marker='o', linestyle='-', label=algo)
plt.title('Average Comparisons vs. Input Size')
plt.xlabel('Input Size (n)')
plt.ylabel('Average Comparisons')
plt.xscale('log')
plt.yscale('log')
plt.legend()
plt.grid(True, which="both", ls="--")
plt.savefig('average_comparisons_vs_size.png')
plt.close()
# --- Plot 3: Average Moves vs. Size ---
plt.figure(figsize=(12, 8))
for algo in algorithms:
subset = df[df['Algorithm'] == algo]
plt.plot(subset['Size'], subset['Avg Moves'], marker='o', linestyle='-', label=algo)
plt.title('Average Moves vs. Input Size')
plt.xlabel('Input Size (n)')
plt.ylabel('Average Moves')
plt.xscale('log')
plt.yscale('log')
plt.legend()
plt.grid(True, which="both", ls="--")
plt.savefig('average_moves_vs_size.png')
plt.close()
print("Plots saved as average_time_vs_size.png, average_comparisons_vs_size.png, and average_moves_vs_size.png")