-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent_automator.py
More file actions
120 lines (98 loc) · 3.79 KB
/
content_automator.py
File metadata and controls
120 lines (98 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import random
import time
import copy
import sys
# Increase recursion depth just in case we test massive arrays with Quick Sort
sys.setrecursionlimit(10000)
# --- 1. The Sorting Algorithms ---
def quick_sort(arr):
"""A standard recursive Quick Sort implementation."""
if len(arr) <= 1:
return arr
pivot = arr[len(arr) // 2]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return quick_sort(left) + middle + quick_sort(right)
def merge_sort(arr):
"""A standard recursive Merge Sort implementation."""
if len(arr) <= 1:
return arr
mid = len(arr) // 2
left = merge_sort(arr[:mid])
right = merge_sort(arr[mid:])
return merge(left, right)
def merge(left, right):
"""Helper function for Merge Sort."""
result = []
i = j = 0
while i < len(left) and j < len(right):
if left[i] < right[j]:
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1
result.extend(left[i:])
result.extend(right[j:])
return result
def python_built_in_sort(arr):
"""Python's highly optimized built-in Timsort."""
arr.sort()
return arr
# --- 2. The Benchmarking Engine ---
def benchmark_algorithm(algo_name, algorithm_func, data):
"""
Runs a sorting algorithm and measures exactly how long it takes.
"""
print(f"🔄 Running {algo_name}...")
start_time = time.perf_counter()
algorithm_func(data)
end_time = time.perf_counter()
# Calculate time in milliseconds
elapsed_time = (end_time - start_time) * 1000
return elapsed_time
# --- 3. Interactive Application Loop ---
def main():
print("\n" + "="*55)
print("⏱️ DYNAMIC ALGORITHM RUNTIME BENCHMARKER ⏱️")
print("="*55)
print("Type 'quit' at any prompt to exit.\n")
while True:
# 1. Get dynamic array size
user_input = input("\nEnter the size of the array to test (e.g., 10000): ")
if user_input.lower() == 'quit':
break
try:
array_size = int(user_input)
if array_size < 1:
print("⚠️ Please enter a positive number.")
continue
except ValueError:
print("❌ Invalid input. Please enter a whole number.")
continue
print(f"\n[Generating array of {array_size:,} random integers...]")
# 2. Generate the master dataset
# We use numbers between 1 and the array size to ensure a good mix of duplicates and unique values
master_array = [random.randint(1, array_size) for _ in range(array_size)]
# 3. Create deep copies so every algorithm sorts the exact same unsorted data
data_for_quick = copy.deepcopy(master_array)
data_for_merge = copy.deepcopy(master_array)
data_for_builtin = copy.deepcopy(master_array)
# 4. Run the benchmarks
print("-" * 55)
quick_time = benchmark_algorithm("Quick Sort", quick_sort, data_for_quick)
merge_time = benchmark_algorithm("Merge Sort", merge_sort, data_for_merge)
builtin_time = benchmark_algorithm("Python Built-in (Timsort)", python_built_in_sort, data_for_builtin)
# 5. Display the Results Dashboard
print("\n" + "="*55)
print(f"📊 RESULTS FOR {array_size:,} ITEMS (Time in Milliseconds) 📊")
print("="*55)
print(f"{'Algorithm':<25} | {'Execution Time (ms)':<20}")
print("-" * 55)
print(f"{'Quick Sort':<25} | {quick_time:>15.2f} ms")
print(f"{'Merge Sort':<25} | {merge_time:>15.2f} ms")
print(f"{'Python Built-in':<25} | {builtin_time:>15.2f} ms")
print("="*55 + "\n")
if __name__ == "__main__":
main()