├── README.md └── sortingnew.py /README.md: -------------------------------------------------------------------------------- 1 | # SORTING ALGORITHM VISUALIZER 2 | Sorting Visualizer: A Python project with a graphical interface using tkinter to demonstrate Insertion, Selection, and Bubble Sort algorithms step-by-step on an array. Adjustable speed control for better visualization. 3 | 4 | 5 | Project Description: Sorting Visualizer in Python 6 | 7 | In this project, we will create a Sorting Visualizer using Python and three popular sorting algorithms: Insertion Sort, Selection Sort, and Bubble Sort. The Sorting Visualizer will provide a graphical representation of how these sorting algorithms work step-by-step, making it easier to understand their sorting process. 8 | 9 | Key Features: 10 | 1. Graphical Interface: We will create a graphical user interface (GUI) using a Python library like Tkinter or Pygame. The GUI will display an array of bars representing the elements to be sorted. 11 | 12 | 2. Random Array Generation: The Sorting Visualizer will allow users to generate a random array of integers. This array will serve as the input data for sorting. 13 | 14 | 3. Visualization: The main focus of this project is to visually demonstrate how the sorting algorithms work. As the algorithms sort the array, the GUI will update the bars' positions to reflect the current state of the array after each step. 15 | 16 | 4. Step-by-Step Execution: The Sorting Visualizer will execute the sorting algorithms step-by-step, allowing users to observe each algorithm's behavior closely. 17 | 18 | 5. Sorting Algorithm Options: Users will be able to choose which sorting algorithm they want to visualize: Insertion Sort, Selection Sort, or Bubble Sort. 19 | 20 | 6. Adjustable Speed: The visualizer will have an adjustable speed control so that users can slow down or speed up the sorting process for better observation. 21 | 22 | 7. Color Indications: The bars representing the array elements will have different colors to indicate their current status (e.g., unsorted, being compared, swapped). 23 | 24 | Implementation Steps: 25 | 26 | 1. Set up the graphical user interface (GUI) using Tkinter or Pygame, depending on your preference and experience. 27 | 28 | 2. Implement functions to generate a random array of integers and display the bars representing the array elements on the GUI. 29 | 30 | 3. Implement the three sorting algorithms: Insertion Sort, Selection Sort, and Bubble Sort. 31 | 32 | 4. For each sorting algorithm, modify the algorithm's code to include steps that update the GUI after each comparison and swap operation. 33 | 34 | 5. Create buttons or a dropdown menu in the GUI to allow users to choose the sorting algorithm they want to visualize. 35 | 36 | 6. Implement a speed control feature that adjusts the delay between each step of the sorting process. 37 | 38 | 7. Run the sorting algorithm step-by-step and update the GUI at each step to show the current state of the array. 39 | 40 | 8. Test the Sorting Visualizer with various input array sizes and observe how the algorithms work in different scenarios. 41 | 42 | Optional Enhancements: 43 | 44 | 1. Add more sorting algorithms to the visualizer, such as Merge Sort, Quick Sort, etc. 45 | 46 | 2. Include additional data structures like linked lists or trees and visualize how the algorithms sort these structures. 47 | 48 | 3. Allow users to input their custom array for sorting instead of generating a random array. 49 | 50 | 4. Add sound effects to make the visualization more engaging. 51 | 52 | Remember to keep the code well-documented and add comments wherever necessary to explain the logic and steps taken in the sorting algorithms and visualization process. Happy coding! 53 | -------------------------------------------------------------------------------- /sortingnew.py: -------------------------------------------------------------------------------- 1 | #Import the required Libraries 2 | from tkinter import * 3 | import tkinter as tk 4 | import random 5 | import time 6 | 7 | 8 | 9 | #accepting value: 10 | 11 | #n =int(input("Enter Total Number of Elements: ")) 12 | 13 | #Function to swap two bars that will be animated 14 | def swap(pos_0, pos_1): 15 | 16 | bar11, _, bar12, _ = canvas.coords(pos_0) 17 | bar21, _, bar22, _ = canvas.coords(pos_1) 18 | canvas.move(pos_0, bar21-bar11, 0) 19 | canvas.move(pos_1, bar12-bar22, 0) 20 | 21 | 22 | worker = None 23 | 24 | #Insertion Sort 25 | def _insertion_sort(): 26 | global barList 27 | global lengthList 28 | 29 | for i in range(len(lengthList)): 30 | cursor = lengthList[i] 31 | cursorBar = barList[i] 32 | pos = i 33 | 34 | while pos > 0 and lengthList[pos - 1] > cursor: 35 | lengthList[pos] = lengthList[pos - 1] 36 | barList[pos], barList[pos - 1] = barList[pos - 1], barList[pos] 37 | swap(barList[pos],barList[pos-1]) 38 | yield 39 | pos -= 1 40 | 41 | lengthList[pos] = cursor 42 | barList[pos] = cursorBar 43 | swap(barList[pos],cursorBar) 44 | 45 | 46 | #Bubble Sort 47 | def _bubble_sort(): 48 | global barList 49 | global lengthList 50 | 51 | for i in range(len(lengthList) - 1): 52 | for j in range(len(lengthList) - i - 1): 53 | if(lengthList[j] > lengthList[j + 1]): 54 | lengthList[j] , lengthList[j + 1] = lengthList[j + 1] , lengthList[j] 55 | barList[j], barList[j + 1] = barList[j + 1] , barList[j] 56 | swap(barList[j + 1] , barList[j]) 57 | yield 58 | 59 | 60 | #Selection Sort 61 | def _selection_sort(): 62 | global barList 63 | global lengthList 64 | 65 | for i in range(len(lengthList)): 66 | min = i 67 | time.sleep(0.5) 68 | for j in range(i + 1 ,len(lengthList)): 69 | if(lengthList[j] < lengthList[min]): 70 | min = j 71 | lengthList[min], lengthList[i] = lengthList[i] ,lengthList[min] 72 | barList[min] , barList[i] = barList[i] , barList[min] 73 | 74 | swap(barList[min] , barList[i]) 75 | 76 | yield 77 | 78 | 79 | #Triggering Fuctions 80 | 81 | def insertion_sort(): 82 | global worker 83 | worker = _insertion_sort() 84 | animate() 85 | 86 | def selection_sort(): 87 | global worker 88 | worker = _selection_sort() 89 | animate() 90 | 91 | def bubble_sort(): 92 | global worker 93 | worker = _bubble_sort() 94 | animate() 95 | 96 | 97 | 98 | #Animation Function 99 | def animate(): 100 | global worker 101 | if worker is not None: 102 | try: 103 | next(worker) 104 | window.after(10, animate) 105 | except StopIteration: 106 | worker = None 107 | finally: 108 | window.after_cancel(animate) 109 | 110 | 111 | #Generator function for generating data 112 | def generate(): 113 | global barList 114 | global lengthList 115 | canvas.delete('all') 116 | barstart = 5 117 | barend = 15 118 | barList = [] 119 | lengthList = [] 120 | 121 | #Creating a rectangle 122 | for bar in range(0, (number)): 123 | randomY = random.randint(1, 360) 124 | bar = canvas.create_rectangle(barstart, randomY, barend, 365, fill='yellow') 125 | barList.append(bar) 126 | barstart += 10 127 | barend += 10 128 | 129 | #Getting length of the bar and appending into length list 130 | for bar in barList: 131 | bar = canvas.coords(bar) 132 | length = bar[3] - bar[1] 133 | lengthList.append(length) 134 | 135 | #Maximum is colored Red 136 | #Minimum is colored Black 137 | for i in range(len(lengthList)-1): 138 | if lengthList[i] == min(lengthList): 139 | canvas.itemconfig(barList[i], fill='red') 140 | elif lengthList[i] == max(lengthList): 141 | canvas.itemconfig(barList[i], fill='black') 142 | 143 | 144 | #for Accepting total number of Inputs 145 | def Accept_value(): 146 | global number 147 | t1=int(a.get()) 148 | number = t1 149 | 150 | 151 | 152 | #Main Code(Driver Code) 153 | 154 | 155 | root = Tk() 156 | # specify size of window. 157 | root.geometry("700x350") 158 | # Create text widget and specify size. 159 | T = Text(root, height = 5, width = 52,bg = "yellow") 160 | # Create label 161 | l = Label(root, text = "SORTING ALGORITHM VISUALIZER") 162 | l.config(font =("Courier", 14)) 163 | 164 | Fact = """MINI PROJECT 165 | Group Members are :- 166 | ADITYA SURYAWANSHI 167 | MIHIR SONAWANE""" 168 | 169 | 170 | # Create an Exit button. 171 | b2 = Button(root, text = "NEXT", 172 | command = root.destroy) 173 | 174 | l.pack() 175 | T.pack() 176 | b2.pack() 177 | 178 | # Insert The Fact. 179 | T.insert(tk.END, Fact) 180 | root.mainloop() 181 | 182 | #ACCEPT NUMBER OF Input 183 | win = tk.Tk() 184 | win.geometry("700x350") 185 | # Create an Entry widget 186 | a=Entry(win, width=35) 187 | a.pack() 188 | Button(win, text="Enter number of Display BARS from", command=Accept_value).pack() 189 | Button(win, text="Next", command=win.destroy).pack() 190 | 191 | win.mainloop() 192 | 193 | 194 | 195 | 196 | #Making a window using the Tk widget 197 | window = tk.Tk() 198 | window.title('Sorting Visualizer') 199 | window.geometry('1000x450') 200 | 201 | 202 | #Making a Canvas within the window to display contents 203 | canvas = tk.Canvas(window, width='1000', height='400') 204 | canvas.grid(column=0,row=0, columnspan = 50) 205 | 206 | #Buttons 207 | insert = tk.Button(window, text='Insertion Sort', command=insertion_sort) 208 | select = tk.Button(window, text='Selection Sort', command=selection_sort) 209 | bubble = tk.Button(window, text='Bubble Sort', command=bubble_sort) 210 | shuf = tk.Button(window, text='Shuffle', command=generate) 211 | insert.grid(column=1,row=1) 212 | select.grid(column=2,row=1) 213 | bubble.grid(column=3,row=1) 214 | shuf.grid(column=0, row=1) 215 | 216 | generate() 217 | window.mainloop() 218 | 219 | 220 | 221 | 222 | --------------------------------------------------------------------------------