Skip to content

Instantly share code, notes, and snippets.

@luiscosio
Created September 9, 2024 17:16
Show Gist options
  • Select an option

  • Save luiscosio/fc22d01c4acc8c5abeeeff95ad903373 to your computer and use it in GitHub Desktop.

Select an option

Save luiscosio/fc22d01c4acc8c5abeeeff95ad903373 to your computer and use it in GitHub Desktop.
import numpy as np
import matplotlib.pyplot as plt
# Define the range of input sizes to calculate performance
values = np.linspace(1, 100, 100)
# Define the performance functions for both algorithms
def algorithm_1(n):
return n**3 # Cubic time complexity O(n^3)
def algorithm_2_best_case(n):
return n**2 # Quadratic time complexity O(n^2)
def algorithm_2_worst_case(n):
return n**4 # Quartic time complexity O(n^4)
# Calculate performance for each algorithm over the range of input sizes
algorithm_1_performance = algorithm_1(values)
algorithm_2_best_case_performance = algorithm_2_best_case(values)
algorithm_2_worst_case_performance = algorithm_2_worst_case(values)
# Plot the performance of both algorithms
plt.figure(figsize=(10, 6))
plt.plot(values, algorithm_1_performance, label="Algorithm 1: O(n^3)", color='blue')
plt.plot(values, algorithm_2_best_case_performance, label="Algorithm 2 best case: O(n^2)", color='green')
plt.plot(values, algorithm_2_worst_case_performance, label="Algorithm 2 worst case: O(n^4)", color='pink')
# Labeling the plot
plt.title("Performance comparison of Algorithm 1 and Algorithm 2 for SGA")
plt.xlabel("Input size")
plt.ylabel("Operations")
plt.legend()
plt.grid(True)
# Display the plot
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment