diff --git a/README.md b/README.md index d4c5f8d..029adf3 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Sorting-Algorithms-Visualizer -Program made with Python and Pygame module for visualizing sorting algorithms \ +This project is a Python application that visualizes different sorting algorithms using Pygame. It is great for students, developers, and educators who want to learn how sorting algorithms work through animation.\ Support this project by leaving a :star: | | | | @@ -14,3 +14,27 @@ Support this project by leaving a :star: - Clone GitHub repository `git clone https://github.com/LucasPilla/Sorting-Algorithms-Visualizer.git` - Install requirements: `pip3 install -r requirements.txt` - Run: `python3 src/main.py` + +## How to use +1. Enter appropriate number of bars required in the text box +2. Pick appropriate sorting algorithm from the dropdown for the visualizer +3. Use the play/pause button to control the visuals + +## Supported Algorithms - Average Time Complexities +- Cycle Sort - O(n^2) +- Cocktail Sort - O(n^2) +- Counting Sort - O(n + k) +- Quick Sort - O( n log n) +- Merge Sort - O(n log n) +- Selection Sort - O(n^2) +- Bubble Sort - O(n^2) +- Insertion Sort - O(n^2) +- Bogo Sort - O((n-1)!) +- Heap Sort - O(n log n) +- Shell Sort - O(n log n) +- Radix Sort - O(nk) +- Bucket Sort - (n + k) +- Pancake Sort - O(n^2) + +## Licensing +- This project is licensed under the MIT License diff --git a/src/main.py b/src/main.py index b56de7d..36f1356 100644 --- a/src/main.py +++ b/src/main.py @@ -23,6 +23,16 @@ screen = pygame.display.set_mode((900, 500)) window = Window(screen) +# Load images and get their sizes +slow_image = pygame.image.load('res/button_slow.png') +slow_width, slow_height = slow_image.get_size() + +normal_image = pygame.image.load('res/button_normal.png') +normal_width, normal_height = normal_image.get_size() + +fast_image = pygame.image.load('res/button_fast.png') +fast_width, fast_height = fast_image.get_size() + window.add_widget( widget_id = 'size_input', widget = TextBox((30, 440, 100, 50), 'Size', grey, baseFont, '100') @@ -35,6 +45,24 @@ widget_id = 'play_button', widget = ButtonBox((350, 440, 40, 40), 'res/playButton.png', 'res/stopButton.png') ) +# Positions for speed buttons +slow_x = 425 +normal_x = slow_x + slow_width + 20 # 20 pixels spacing +fast_x = normal_x + normal_width + 20 + +# Add speed buttons with accurate dimensions +window.add_widget( + widget_id='slow_button', + widget=ButtonBox((slow_x, 438, slow_width, slow_height), 'res/button_slow.png', 'res/button_slow.png') +) +window.add_widget( + widget_id='normal_button', + widget=ButtonBox((normal_x, 438, normal_width, normal_height), 'res/button_normal.png', 'res/button_normal.png') +) +window.add_widget( + widget_id='fast_button', + widget=ButtonBox((fast_x, 438, fast_width, fast_height), 'res/button_fast.png', 'res/button_fast.png') +) def drawBars(screen, array, redBar1, redBar2, blueBar1, blueBar2, greenRows = {}): '''Draw the bars and control their colors''' @@ -56,6 +84,15 @@ def main(): isPlaying = False isSorting = False sortingIterator = None + visualDelay = 100 # delay is set to 100 by default + current_speed = 'normal' + + # speed modes in milliseconds + speedModes = { + 'slow' : 250, # delay is set to 200 in slow mode + 'normal' : 100, # delay is set to 100 is normal mode + 'fast' : 20 # delay is set to 20 in fast mode + } while running: screen.fill(white) @@ -65,8 +102,26 @@ def main(): window.update(event) + # constantly check for mode changes + if window.get_widget_value('slow_button'): + if current_speed != 'slow': + current_speed = 'slow' + visualDelay = speedModes[current_speed] + print("slow button is pressed") + elif window.get_widget_value('normal_button'): + if current_speed != 'normal': + current_speed = 'normal' + visualDelay = speedModes[current_speed] + print("normal button is pressed") + elif window.get_widget_value('fast_button'): + if current_speed != 'fast': + current_speed = 'fast' + visualDelay = speedModes[current_speed] + print("fast button is pressed") + isPlaying = window.get_widget_value('play_button') - if isPlaying and not isSorting: + if isPlaying and not isSorting: + print("play button is pressed") # random list to be sorted numBars = int(window.get_widget_value('size_input')) numbers = [randint(10, 400) for i in range(numBars)] @@ -76,13 +131,17 @@ def main(): sortingIterator = algorithmsDict[sortingAlgorithm](numbers, 0, numBars-1) isSorting = True - if not isPlaying: + if not isPlaying and isSorting: + current_speed = 'normal' + visualDelay = speedModes[current_speed] isSorting = False + if isSorting: try: numbers, redBar1, redBar2, blueBar1, blueBar2 = next(sortingIterator) drawBars(screen, numbers, redBar1, redBar2, blueBar1, blueBar2) + pygame.time.delay(visualDelay) # control speed of sorting visuals except StopIteration: isSorting = False window.set_widget_value('play_button', False) diff --git a/res/bubble_sort.gif b/src/res/bubble_sort.gif similarity index 100% rename from res/bubble_sort.gif rename to src/res/bubble_sort.gif diff --git a/res/bucket_sort.gif b/src/res/bucket_sort.gif similarity index 100% rename from res/bucket_sort.gif rename to src/res/bucket_sort.gif diff --git a/res/cocktail_sort.gif b/src/res/cocktail_sort.gif similarity index 100% rename from res/cocktail_sort.gif rename to src/res/cocktail_sort.gif diff --git a/res/counting_sort.gif b/src/res/counting_sort.gif similarity index 100% rename from res/counting_sort.gif rename to src/res/counting_sort.gif diff --git a/res/heap_sort.gif b/src/res/heap_sort.gif similarity index 100% rename from res/heap_sort.gif rename to src/res/heap_sort.gif diff --git a/res/insertion_sort.gif b/src/res/insertion_sort.gif similarity index 100% rename from res/insertion_sort.gif rename to src/res/insertion_sort.gif diff --git a/res/merge_sort.gif b/src/res/merge_sort.gif similarity index 100% rename from res/merge_sort.gif rename to src/res/merge_sort.gif diff --git a/res/playButton.png b/src/res/playButton.png similarity index 100% rename from res/playButton.png rename to src/res/playButton.png diff --git a/res/preview.gif b/src/res/preview.gif similarity index 100% rename from res/preview.gif rename to src/res/preview.gif diff --git a/res/quick_sort.gif b/src/res/quick_sort.gif similarity index 100% rename from res/quick_sort.gif rename to src/res/quick_sort.gif diff --git a/res/selectAlgo.gif b/src/res/selectAlgo.gif similarity index 100% rename from res/selectAlgo.gif rename to src/res/selectAlgo.gif diff --git a/res/selection_sort.gif b/src/res/selection_sort.gif similarity index 100% rename from res/selection_sort.gif rename to src/res/selection_sort.gif diff --git a/res/stopButton.png b/src/res/stopButton.png similarity index 100% rename from res/stopButton.png rename to src/res/stopButton.png