-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
91 lines (71 loc) · 2.94 KB
/
example.py
File metadata and controls
91 lines (71 loc) · 2.94 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
#!/usr/bin/env python3
"""
Quick usage example for the Advanced Sorting Algorithm Visualizer
This script demonstrates how to use the visualizer programmatically
"""
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from main import *
import pygame
def example_usage():
"""Example of how to use the visualizer components"""
pygame.init()
print("🎯 Advanced Sorting Algorithm Visualizer - Usage Example")
print("=" * 60)
# Create a small array for demonstration
test_array = [64, 34, 25, 12, 22, 11, 90]
print(f"Original array: {test_array}")
# Initialize the visualizer
draw_info = DrawInformation(800, 600, test_array)
# Example 1: Generate different types of arrays
print("\n📊 Array Generation Examples:")
random_array = generate_list(10, 1, 50, "random")
print(f"Random array: {random_array}")
sorted_array = generate_list(10, 1, 50, "sorted")
print(f"Sorted array: {sorted_array}")
reverse_array = generate_list(10, 1, 50, "reverse")
print(f"Reverse array: {reverse_array}")
nearly_sorted = generate_list(10, 1, 50, "nearly_sorted")
print(f"Nearly sorted: {nearly_sorted}")
# Example 2: Theme switching
print("\n🎨 Theme Examples:")
for theme in Theme:
colors = ColorScheme.get_theme(theme)
print(f"{theme.value.title()} theme - Background: {colors.background}, Text: {colors.text}")
# Example 3: Algorithm complexity information
print("\n⚡ Algorithm Complexity:")
algorithms_info = {
"Bubble Sort": "O(n²) - Simple but inefficient",
"Insertion Sort": "O(n²) - Good for small arrays",
"Selection Sort": "O(n²) - Simple selection-based",
"Merge Sort": "O(n log n) - Divide and conquer",
"Quick Sort": "O(n log n) avg - Efficient pivot-based",
"Heap Sort": "O(n log n) - Heap-based sorting"
}
for algo, desc in algorithms_info.items():
print(f"• {algo}: {desc}")
# Example 4: Statistics tracking
print("\n📈 Statistics Tracking:")
stats = SortingStats()
stats.start_sorting()
stats.comparisons = 15
stats.swaps = 8
stats.array_accesses = 30
stats.end_sorting()
print(f"Example stats:")
print(f" Comparisons: {stats.comparisons}")
print(f" Swaps: {stats.swaps}")
print(f" Array accesses: {stats.array_accesses}")
print(f" Time elapsed: {stats.get_elapsed_time():.3f}s")
print("\n✨ Usage Tips:")
print("• Use SPACE to start/pause sorting")
print("• Press 1-6 to select different algorithms")
print("• Use T to cycle through themes")
print("• Press S for step-by-step mode")
print("• Use ↑/↓ to adjust speed")
print("• Press R to reset with new random array")
print("\n🚀 Ready to start! Run 'python main.py' to begin.")
pygame.quit()
if __name__ == "__main__":
example_usage()