Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 49 additions & 23 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,61 @@
import sys
import tkinter as tk
from tkinter import ttk
import sorting as s
import visualizer as vs
import numpy as np
import random

class SortingVisualizerApp:
def __init__(self, master):
self.master = master
self.master.title("Sorting Algorithm Visualizer")

self.array_size = 100
self.algorithm_var = tk.StringVar()

self.create_widgets()

def create_widgets(self):
# Algorithm selection
algorithm_label = tk.Label(self.master, text="Select Sorting Algorithm:")
algorithm_label.place(x=30,y=10)

algorithm_combobox = ttk.Combobox(self.master, textvariable=self.algorithm_var,
values=['bubble_sort', 'heap_sort', 'selection_sort', 'insertion_sort', 'quick_sort', 'merge_sort'])
algorithm_combobox.place(x=30,y=30)

# Start button
start_button = tk.Button(self.master, text="Start Visualization", command=self.start_visualization, border= 3)
start_button.place(x=45,y=60)

l1 =tk.Label(self.master,text="A project made by \nAyush and Kaamil",border=1)
l1.place(x=42,y=90)

def start_visualization(self):
selected_algorithm = self.algorithm_var.get()
if selected_algorithm:
algorithm = getattr(s, selected_algorithm, None)

if algorithm is None:
tk.messagebox.showerror("Error", "Invalid algorithm selection.")
return

arr = s.Array(get_random_array(self.array_size))
algorithm(arr)

plotter = vs.Plotter(arr.pile, title=selected_algorithm, repeat=True, interval=2)
plotter.plot()

def get_random_array(length):
n = list(range(length))
random.shuffle(n)
return n

def main():
root = tk.Tk()
app = SortingVisualizerApp(root)
root.geometry("200x130")
root.mainloop()

array_size = 50

algorithms = [s.bubble_sort, s.heap_sort, s.selection_sort,
s.insertion_sort, s.quick_sort, s.insertion_sort, s.merge_sort]

if len(sys.argv) < 2:
print('Usage:', 'python', sys.argv[0], 'function_name', '\n')
print('Choose from:', '\n')
for algorithm in algorithms:
print(algorithm.__name__)
print('\n')
sys.exit(0)

algorithms = list(filter(lambda a: a.__name__ == sys.argv[1], algorithms))
if (len(algorithms) == 0):
print('Method does not exist.')
else:
algorithm = algorithms[0]
arr = s.Array(get_random_array(array_size))
algorithm(arr)
plotter = vs.Plotter(arr.pile, title=algorithm.__name__, repeat=True, interval=1)
plotter.plot()
if __name__ == "__main__":
main()